Simulate key press

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

paulclaude
Posts: 121
Joined: Thu Mar 27, 2008 10:19 am

Simulate key press

Post by paulclaude » Wed Mar 09, 2016 7:11 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10322
Joined: Wed May 06, 2009 2:28 pm

Re: Simulate key press

Post by dunbarx » Wed Mar 09, 2016 8:47 pm

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

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm

Re: Simulate key press

Post by quailcreek » Wed Mar 09, 2016 9:31 pm

Paul,
Your question is a little vague. What is it that you're trying to accomplish?
Tom
MacBook Pro OS Mojave 10.14

paulclaude
Posts: 121
Joined: Thu Mar 27, 2008 10:19 am

Re: Simulate key press

Post by paulclaude » Thu Mar 10, 2016 10:53 am

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

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Simulate key press

Post by Simon » Thu Mar 10, 2016 12:12 pm

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

paulclaude
Posts: 121
Joined: Thu Mar 27, 2008 10:19 am

Re: Simulate key press

Post by paulclaude » Thu Mar 10, 2016 12:49 pm

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10322
Joined: Wed May 06, 2009 2:28 pm

Re: Simulate key press

Post by dunbarx » Thu Mar 10, 2016 5:49 pm

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
Last edited by dunbarx on Thu Mar 10, 2016 6:26 pm, edited 2 times in total.

paulclaude
Posts: 121
Joined: Thu Mar 27, 2008 10:19 am

Re: Simulate key press

Post by paulclaude » Thu Mar 10, 2016 6:14 pm

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:

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10322
Joined: Wed May 06, 2009 2:28 pm

Re: Simulate key press

Post by dunbarx » Thu Mar 10, 2016 6:30 pm

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

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Simulate key press

Post by jacque » Fri Mar 11, 2016 5:51 am

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10322
Joined: Wed May 06, 2009 2:28 pm

Re: Simulate key press

Post by dunbarx » Sat Mar 12, 2016 12:56 am

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

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Simulate key press

Post by [-hh] » Sat Mar 12, 2016 6:15 am

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]
shiftLock happens

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Simulate key press

Post by jacque » Sat Mar 12, 2016 6:16 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10322
Joined: Wed May 06, 2009 2:28 pm

Re: Simulate key press

Post by dunbarx » Sat Mar 12, 2016 6:25 pm

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

paulclaude
Posts: 121
Joined: Thu Mar 27, 2008 10:19 am

Re: Simulate key press

Post by paulclaude » Sat Mar 12, 2016 6:44 pm

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

Post Reply