Page 1 of 2

Simulate key press

Posted: Wed Mar 09, 2016 7:11 pm
by paulclaude
Maybe it's a dumb question, but there's a way to simulate a key press (up arrow, code 65362) on a unlocked text field? "Type" doesn't works and calling Applescript is too slow.

Thanks

Re: Simulate key press

Posted: Wed Mar 09, 2016 8:47 pm
by dunbarx
Hi.

Do you mean to do something in a field that normally is not available directly? Like:

Code: Select all

on rawkeydown tkey
   if tkey is "65362" then
      get word 2 of the selectedLine of fld 1
      select line (it - 1) of fld 1
   end if
end rawkeydown
This in a field or card script. The line is selected as opposed to simply having the selection point moved up a line. Or do you mean from outside the field entirely, as if you clicked a button and emulated doing something in a field?

Craig Newman

Re: Simulate key press

Posted: Wed Mar 09, 2016 9:31 pm
by quailcreek
Paul,
Your question is a little vague. What is it that you're trying to accomplish?

Re: Simulate key press

Posted: Thu Mar 10, 2016 10:53 am
by paulclaude
I should move the cursor on the text of a field as the user phisically press an arrow key.

I apologize that my english it's not very good, I'll try to give an example:

the cursor is in red:

"The quick brown fox jumps over the lazy dog"
is an English-language pangram—a phrase that
contains all of the letters of| the alphabet. It is
commonly used for touch-typing practice.

the cursor is moved by the script to the upper line:
"The quick brown fox jumps over the lazy dog"
is an English-language pan|gram—a phrase that
contains all of the letters of the alphabet. It is
commonly used for touch-typing practice.

I can accomplish this with a line of code applescript, but this way is very slow:

Code: Select all

do "tell application " & quote & "System Events" & quote & " to keystroke (ASCII character 30) --up arrow" as AppleScript
I cannot "calculate" the position counting chars and using "select", as the upper row may have different fonts, styles or sizes.

That's all...

Re: Simulate key press

Posted: Thu Mar 10, 2016 12:12 pm
by Simon
Hi paulclaude,
I cannot "calculate" the position counting chars and using "select", as the upper row may have different fonts, styles or sizes.
Counting the characters and the font size/style are independent of each other.

Code: Select all

put char 2 of fld 1 
will always put the second character of field 1 no matter what the font is.
I'm not sure why you are using "select".

There may be more that you haven't said yet.
"the quiet brown fox..."
Are you also checking to see if the content is correct?

So, more questions for you.

Simon

Re: Simulate key press

Posted: Thu Mar 10, 2016 12:49 pm
by paulclaude
Sorry, I should be more precise: I need to move the cursor EXACTLY over the selected char of the below row, regardless of fonts, sizes or styles in the top row. You can do this by pressing the up arrow key. I need to do this by script.

Re: Simulate key press

Posted: Thu Mar 10, 2016 5:49 pm
by dunbarx
You know, I am not as agile as I think, and I bet Jacque will simply tell me the simple answer.

I cannot invoke an "arrowKey" message from afar. Nor can I get a "rawkeyUp" message to work. All this from a button handler, with a cursor blinking in a field

This kluge works, provided one has set the textHeight of of the field

Code: Select all

on mouseUp
   select after char 12 of line 3 of fld 1
   get the selectedLoc
   wait 15
   click at item 1 of it  & "," & (item 2 of it - the textHeight of fld 1)
end mouseUp
But this ought to be so straightforward...

Craig

Re: Simulate key press

Posted: Thu Mar 10, 2016 6:14 pm
by paulclaude
dunbarx wrote:You know, I am not as agile as I think, and I bet Jacque will simply tell me the simple answer.

I cannot invoke an "arrowKey" message from afar. Nor can I get a "rawkeyUp" message to work. All this from a button handler, with a cursor blinking in a field

This kluge works, provided one has set the textHeight of of the field

Code: Select all

on mouseUp
   select after char 12 of line 3 of fld 1
   get the selectedLoc
   wait 15
   click at item 1 of it  & "," & item 2 of it - the textHeightof fld 1
end mouseUp
This ought to be so straightforward...

Craig

Thanks, Craig, this is what I needed :wink:

Re: Simulate key press

Posted: Thu Mar 10, 2016 6:30 pm
by dunbarx
Paul.

Good. I am sure you can make it universal for all directions. You would likely use the "selectedChunk" to move left and right.

But it is a kluge. Straight message wrangling ought to be the right way. Anyone?

Craig

Re: Simulate key press

Posted: Fri Mar 11, 2016 5:51 am
by jacque
dunbarx wrote: I bet Jacque will simply tell me the simple answer.
Nope, I was going to suggest selectedLoc too. I can't think of any other way to do it.

Re: Simulate key press

Posted: Sat Mar 12, 2016 12:56 am
by dunbarx
Jacque.

Hmmm

What is it then about the arrowKey message that resists external invocation? A little clever fooling around has always alllowed some sort of method. Though I admit that

Code: Select all

click at the loc of some object/someChunk
is a kluge.

I tried sending "arrowKey", several attempts with both raw and cooked keyUp/down handlers, nada. I could not get anything to fire.

Craig

Re: Simulate key press

Posted: Sat Mar 12, 2016 6:15 am
by [-hh]
Hi all,


@Craig.
I tried several such things you speak of when emulating "typing" for HTML5 ("Wake up Neo"): I didn't find a chance to emulate an arrowkey stroke (even when navigationArrows are set to false), not by keydown, rawkeydown, arrowkey (and, in the IDE, not by type). One could try to emulate the same what a text engine does (see below).


@paulclaude:
What you want and what arrowkey up does may be slightly different things if we have lines of different lengths:

Make a textfield fld 1
set dontwrap of fld 1 o true
set textFont of fld 1 to "Courier"
Put the following into fld 1.

abc
de
f
gh ijk
lmnopqrstuvwxyz

Now click anywhere and go up with the arrowkey.
You'll see non-systematic behavior and also slight differences between LC 6/7/8. This doesn't really matter if you have equal line lengths.

What you want is to go up one line and select after the same char number, if possible, else after the last char of that line. Right?

That is in the example above:
from after char 3 in line 5
= to after char 3 in line 4
= to after char 1 in line 3
= to after char 1 in line 2
= to after char 1 in line 1

Right? This is done by the following script in fld 1. You can send "goUp" from anywhere to fld 1 if your action doesn't steal focus from fld 1.

Code: Select all

on goUp
  -- focus on me #<-- may be necessary
  put word 4 of the selectedChunk into c2
  put -1+the num of lines of (char 1 to c2 of fld 1) into nL
  put 1+length(line 1 to nL of fld 1) into c1
  select after char c2-c1 of line nL of fld 1 #<-- OK for LC 6/7
  -- for a workaround for LC 8-dp15 bug: see below
end goUp
WARNING.
I found a bug in LC 8-dp15 by that solution.

If you have a field of several lines and (x=1,2,3,...):

Code: Select all

select char 1 to 100 of line x of fld 1
then "of line x" is "expanded", it selects ALWAYS char 1 to 100 of line x to -1 of fld 1!!

Hermann

[**Edit. Workaround for LC 8-dp15: Select line nL if length(line nL)>c2-c1]

Re: Simulate key press

Posted: Sat Mar 12, 2016 6:16 pm
by jacque
What is it then about the arrowKey message that resists external invocation
I don't know. It's apparently linked directly to the OS message somehow and won't respond without keyboard input. Perhaps we can only directly call LC native messages like mouseUp. Or maybe it's an oversight in the engine.

Re: Simulate key press

Posted: Sat Mar 12, 2016 6:25 pm
by dunbarx
Jacque.

Hmmm again.

Do you think this is worth mentioning? I have always been comforted by believing that an xTalk IDE was so flexible and accessible that, generically, " being able to invoke an arrowKey message under script control" was, simply, doable.

Did I mention that other instances of that pipe dream required kluges?

Craig

Re: Simulate key press

Posted: Sat Mar 12, 2016 6:44 pm
by paulclaude
@paulclaude:
What you want and what arrowkey up does may be slightly different things if we have lines of different lengths:
The upper line may contains different fonts, styles and sizes, as I wrote before. For the problem of different lenght lines, I solved it by adding whitespaces to the upper line before to go up and removing trailing spaces after.