Simulate keypress in a script

Something you want to see in a LiveCode product? Want a new forum set up for a specific topic? Talk about it here.

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 3620
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Simulate keypress in a script

Post by richmond62 » Tue Jul 19, 2016 7:03 pm

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

SparkOut
Posts: 2020
Joined: Sun Sep 23, 2007 4:58 pm

Re: Simulate keypress in a script

Post by SparkOut » Tue Jul 19, 2016 10:29 pm

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6179
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Simulate keypress in a script

Post by dunbarx » Wed Jul 20, 2016 1:24 am

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

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 332
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: Simulate keypress in a script

Post by kaveh1000 » Tue Dec 18, 2018 11:08 pm

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.
Kaveh

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2377
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Simulate keypress in a script

Post by jmburnod » Wed Dec 19, 2018 10:59 am

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

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 332
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: Simulate keypress in a script

Post by kaveh1000 » Wed Dec 19, 2018 3:36 pm

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?
Kaveh

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6179
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Simulate keypress in a script

Post by dunbarx » Wed Dec 19, 2018 4:34 pm

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

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 332
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: Simulate keypress in a script

Post by kaveh1000 » Wed Dec 19, 2018 5:16 pm

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...
Kaveh

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 332
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: Simulate keypress in a script

Post by kaveh1000 » Thu Dec 20, 2018 9:34 pm

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

Code: Select all

send arrowkey "up" to fld 1
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.
Kaveh

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6179
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Simulate keypress in a script

Post by dunbarx » Thu Dec 20, 2018 11:00 pm

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

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 3620
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Simulate keypress in a script

Post by richmond62 » Fri Dec 21, 2018 1:23 pm

ks.png
-

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 36 times

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 332
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: Simulate keypress in a script

Post by kaveh1000 » Fri Dec 21, 2018 4:00 pm

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. :-)
Kaveh

bogs
Posts: 3817
Joined: Sat Feb 25, 2017 10:45 pm

Re: Simulate keypress in a script

Post by bogs » Fri Dec 21, 2018 4:16 pm

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
Image

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 332
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: Simulate keypress in a script

Post by kaveh1000 » Fri Dec 21, 2018 4:51 pm

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.
Kaveh

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6179
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Simulate keypress in a script

Post by dunbarx » Fri Dec 21, 2018 6:15 pm

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

Post Reply

Return to “Feature Requests”