Page 1 of 3
Simulate keypress in a script
Posted: Tue Jul 19, 2016 7:03 pm
by richmond62
Just as one can use a script like this
send "mouseUp" to btn "BLAH"
it would also be useful if one could simulate keypresses like this;
send "keyUp"("x") to card id 2004
send "rawKeyUp"(123) to card id 2004
send "arrowKey"("up") to card id 2004
Re: Simulate keypress in a script
Posted: Tue Jul 19, 2016 10:29 pm
by SparkOut
Yes, but you can abstract:
Card script:
Code: Select all
on keyUp pkey
doKeyUpThing pKey
end keyUp
on doKeyUpThing pKey
answer "do something with" && pKey
end doKeyUpThing
Then instead of sending the keyUp message to the card, you send the doKeyUpThing with whatever parameter you need.
Re: Simulate keypress in a script
Posted: Wed Jul 20, 2016 1:24 am
by dunbarx
Hi.
Try this in a button:
Code: Select all
on mouseUp
answer keyUp("k")
end mouseUp
on mouseUp
send "keyup" && "k" to this card
end mouseUp
And in, say, the card script:
Code: Select all
on keyup tKey
answer tKey
end keyup
function keyUp tKey
return tKey
end keyUp
Each works in its own way.
Craig
Re: Simulate keypress in a script
Posted: Tue Dec 18, 2018 11:08 pm
by kaveh1000
Hi Craig
I came across this clever trick from you. I am trying to do something similar. Wonder if it is possible...
I want to simulate the usual functions of arrow keys in a field, but using a script. For instance, simulate:
- Press right arrow with option key (on mac) – to extend selection to the end of a word
- Press up arrow – to go to nearest word above
Is there a way I can do this?
Of course if there are handlers I can use that is even better, but there a lot of native low level functions that I could use if possible and that would be simpler.
Thanks.
Re: Simulate keypress in a script
Posted: Wed Dec 19, 2018 10:59 am
by jmburnod
Hi Kaveh
I played with arrowkey to select current, prev, next word in a unlocked field, navigationArrows = false.
It works fine except if i use optionkey is down as condition.
Here is the script of a unlocked field called "fText"
Code: Select all
on arrowkey pKey
-- if the optionkey is down then -- doesn't work for up key
if pkey = right then
put getOneWord(the selectedchunk,CurWord ) into tOneWord
select char (item 1 of tOneWord) to (item 2 of tOneWord) of fld "fText"
end if
if pkey = up then
put getOneWord(the selectedchunk,PrevWord) into tOneWord
select word tOneWord of fld "fText"
end if
if pkey = down then
put getOneWord(the selectedchunk,NextWord) into tOneWord
select word tOneWord of fld "fText"
end if
-- end if
end arrowkey
function getOneWord pSelChunk,pMeth
put word 2 of pSelChunk into tStart
repeat with i = tStart down to 1
if char i of fld "fText" = " " then exit repeat
put i into tCurCharS
end repeat
repeat with i = tStart to the num of chars of fld 1
put i into tCurCharD
if char i of fld "fText" = " " then exit repeat
end repeat
if pMeth = "CurWord" then
return tCurCharS & "," & tCurCharD
end if
put char 1 to tCurCharS of fld "fText" into tText
put the num of words of tText into nbW
if pMeth = "PrevWord" then
return nbW-1
end if
if pMeth = "NextWord" then
return nbW+1
end if
end getOneWord
Best regards
Jean-Marc
Re: Simulate keypress in a script
Posted: Wed Dec 19, 2018 3:36 pm
by kaveh1000
Thank you so much Jean-Marc. The macros are very useful.
I was hoping we could somehow tap into what the arrow keys are doing natively already, so actually send a the key arrow-key to the field as if a user pressed that key. The actions that we just take for granted are:
- up arrow goes to nearest char above
- shift extends selection (in 4 directions)
- When extending with shift-right, then shift-left decreases that selection
These are all natively in the system and it is quite a lot of work to simulate them in LiveCode. Any way to taking a shortcut and get LiveCode to imagine a user is pressing the keys?
Re: Simulate keypress in a script
Posted: Wed Dec 19, 2018 4:34 pm
by dunbarx
Hi.
This should be doable. I think that trying to count chars in a field from word 4 of the selectedChunk and navigating to the next or previous line is fraught with peril, because the lengths of each of those lines is likely different.
But try this:
Code: Select all
on arrowkey tKey
switch tKey
case "up"
put item 1 of the selectedLoc into xVal
put item 2 of the selectedLoc into yVal
click at xVal & "," & (yVal - the textHeight of the target)
break
case "down"
...
end switch
end arrowkey
You have to watch out when you are in the first or last line, that sort of thing. And remember that the textHeight of a new field may not be explicitly set to a value.
Craig
Re: Simulate keypress in a script
Posted: Wed Dec 19, 2018 5:16 pm
by kaveh1000
Hi Craig
Your answers are always informative, and even if I knew the handlers I am reminded where to use them!
Yes, going to line above and below is the tricky bit which is why I wanted to rely on the native actions of the cursor that just does it.
let me have another go with these...
Re: Simulate keypress in a script
Posted: Thu Dec 20, 2018 9:34 pm
by kaveh1000
Hi Craig
I have been working around your script and it works fine, but there is a lot of housekeeping to do, e.g. how to test if the cursor is on the top line of text, so we don't click outside.
What I need to do actually is to simulate what the arrow keys do in a field, but from another control, so I wish I could just say something like
or
Code: Select all
send arrowkey "up" with shiftkey down to fld 1
because then the text will be natively selected. And I don't have to worry about being at the top or bottom line. It's all taken care of. Can you think of any way I can simulate an arrow key, because then the system does the job for me.
Re: Simulate keypress in a script
Posted: Thu Dec 20, 2018 11:00 pm
by dunbarx
Something like this:
Code: Select all
on arrowKey tKey
put word 4 of the selectedChunk into tChar
switch tKey
case "right"
if the optionKey is down then
select after word (the number of words in char 1 to tChar of target) of target
end if
break
end switch
end arrowKey
Parentheses just to make that line more readable.
The arrowKey message is just a message. It can be manipulated all day to do whatever you want.
Craig
Re: Simulate keypress in a script
Posted: Fri Dec 21, 2018 1:23 pm
by richmond62
-
Code: Select all
on mouseUp
send "rawKeyUp 108" to btn "xx"
end mouseUp
Re: Simulate keypress in a script
Posted: Fri Dec 21, 2018 4:00 pm
by kaveh1000
Thanks both and thank you Richmond for the stack. I have tried it in my stack and it is working. So I now have in a button:
Code: Select all
on mouseup
select word 3 of fld "text"
send "rawkeyup 65362" to fld "text" -- up arrow
focus fld "text"
end mouseup
And in fld "text" I have:
Code: Select all
on rawKeyUp theKey
if thekey is 65362 then answer "hi"
pass rawKeyUp
end rawKeyUp
and I see "hi" pop up. What I want is that the up arrow moves the cursor to the line above, as would be the case if I pressed arrowkey manually. But it is still showing word 3 selected.
I am missing something fundamental. Enlighten me please.

Re: Simulate keypress in a script
Posted: Fri Dec 21, 2018 4:16 pm
by bogs
kaveh1000 wrote: ↑Fri Dec 21, 2018 4:00 pm
. But it is still showing word 3 selected.
I believe that is because of this line -
on mouseup
select word 3 of fld "text"
send "rawkeyup 65362" to fld "text" -- up arrow
focus fld "text"
end mouseup
Re: Simulate keypress in a script
Posted: Fri Dec 21, 2018 4:51 pm
by kaveh1000
Hi Bogs
Yes, I selected word three, then I am sending an up arrow, so I expect the cursor to move to the line above.
What I am trying to do is to simulate remotely, the actions of arrow keys so (in pseudo-code):
send rightarrow to fld 1 -- moves cursor one char to right
send rightarrow with shift and option key -- selects from cursor to end of word
send uparrow with shift -- Selects from current position to roughly same position on line above
The cursor key is doing all this natively. I just want to do that action remotely, using the native actions of the keys.
Re: Simulate keypress in a script
Posted: Fri Dec 21, 2018 6:15 pm
by dunbarx
What I am trying to do is to simulate remotely, the actions of arrow keys so
If you get my meaning above:
The arrowKey message is just a message. It can be manipulated all day to do whatever you want.
Then you can simulate without actually invoking arrowKey messages remotely. Just do it:
Code: Select all
send "simulateArrows right" to fld 1
on simulateArrows whichArrow
put word 4 of the selectedChunk into tChar
switch whichArrow
case "right"
if the optionKey is down then
select after word (the number of words in char 1 to tChar of target) of target
end if
break
end switch
end simulateArrows
Not tested, but since the selection sticks when you click on a button, it ought to work.
The point here is that maybe we need to abandon the idea of forcing arrowKeys to do our bidding, and just do our bidding ourselves.
Craig