Simulate key press
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 121
- Joined: Thu Mar 27, 2008 10:19 am
Simulate key press
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
Thanks
Re: Simulate key press
Hi.
Do you mean to do something in a field that normally is not available directly? Like:
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
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
Craig Newman
-
- Posts: 746
- Joined: Sun Feb 04, 2007 11:01 pm
Re: Simulate key press
Paul,
Your question is a little vague. What is it that you're trying to accomplish?
Your question is a little vague. What is it that you're trying to accomplish?
Tom
MacBook Pro OS Mojave 10.14
MacBook Pro OS Mojave 10.14
-
- Posts: 121
- Joined: Thu Mar 27, 2008 10:19 am
Re: Simulate key press
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:
I cannot "calculate" the position counting chars and using "select", as the upper row may have different fonts, styles or sizes.
That's all...
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
That's all...
Re: Simulate key press
Hi paulclaude,
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
Counting the characters and the font size/style are independent of each other.I cannot "calculate" the position counting chars and using "select", as the upper row may have different fonts, styles or sizes.
Code: Select all
put char 2 of fld 1
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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
-
- Posts: 121
- Joined: Thu Mar 27, 2008 10:19 am
Re: Simulate key press
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
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
But this ought to be so straightforward...
Craig
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
Craig
Last edited by dunbarx on Thu Mar 10, 2016 6:26 pm, edited 2 times in total.
-
- Posts: 121
- Joined: Thu Mar 27, 2008 10:19 am
Re: Simulate key press
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 fieldThis ought to be so straightforward...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
Craig
Thanks, Craig, this is what I needed

Re: Simulate key press
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
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
Nope, I was going to suggest selectedLoc too. I can't think of any other way to do it.dunbarx wrote: I bet Jacque will simply tell me the simple answer.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: Simulate key press
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 thatis 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
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
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
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.
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,...):
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]
@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
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
Hermann
[**Edit. Workaround for LC 8-dp15: Select line nL if length(line nL)>c2-c1]
shiftLock happens
Re: Simulate key press
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.What is it then about the arrowKey message that resists external invocation
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: Simulate key press
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
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
-
- Posts: 121
- Joined: Thu Mar 27, 2008 10:19 am
Re: Simulate key press
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.@paulclaude:
What you want and what arrowkey up does may be slightly different things if we have lines of different lengths: