Simulate keypress in a script
Moderator: Klaus
-
- Livecode Opensource Backer
- Posts: 10080
- Joined: Fri Feb 19, 2010 10:17 am
Simulate keypress in a script
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
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
Yes, but you can abstract:
Card script:
Then instead of sending the keyUp message to the card, you send the doKeyUpThing with whatever parameter you need.
Card script:
Code: Select all
on keyUp pkey
doKeyUpThing pKey
end keyUp
on doKeyUpThing pKey
answer "do something with" && pKey
end doKeyUpThing
Re: Simulate keypress in a script
Hi.
Try this in a button:
And in, say, the card script:
Each works in its own way.
Craig
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
Code: Select all
on keyup tKey
answer tKey
end keyup
function keyUp tKey
return tKey
end keyUp
Craig
Re: Simulate keypress in a script
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:
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.
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
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.
Kaveh
Re: Simulate keypress in a script
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"
Best regards
Jean-Marc
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
Jean-Marc
https://alternatic.ch
Re: Simulate keypress in a script
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?
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?
Kaveh
Re: Simulate keypress in a script
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:
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
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
Craig
Re: Simulate keypress in a script
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...
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...
Kaveh
Re: Simulate keypress in a script
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
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.
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
Code: Select all
send arrowkey "up" to fld 1
Code: Select all
send arrowkey "up" with shiftkey down to fld 1
Kaveh
Re: Simulate keypress in a script
Something like this:
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
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
The arrowKey message is just a message. It can be manipulated all day to do whatever you want.
Craig
-
- Livecode Opensource Backer
- Posts: 10080
- Joined: Fri Feb 19, 2010 10:17 am
Re: Simulate keypress in a script
Code: Select all
on mouseUp
send "rawKeyUp 108" to btn "xx"
end mouseUp
- Attachments
-
- key sender.livecode.zip
- Here's the stack.
- (703 Bytes) Downloaded 178 times
Re: Simulate keypress in a script
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:
And in fld "text" I have:
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.
Code: Select all
on mouseup
select word 3 of fld "text"
send "rawkeyup 65362" to fld "text" -- up arrow
focus fld "text"
end mouseup
Code: Select all
on rawKeyUp theKey
if thekey is 65362 then answer "hi"
pass rawKeyUp
end rawKeyUp
I am missing something fundamental. Enlighten me please.

Kaveh
Re: Simulate keypress in a script
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
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.
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.
Kaveh
Re: Simulate keypress in a script
If you get my meaning above:What I am trying to do is to simulate remotely, the actions of arrow keys so
Then you can simulate without actually invoking arrowKey messages remotely. Just do it:The arrowKey message is just a message. It can be manipulated all day to do whatever you want.
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
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