StrokesPlus Forum
                       
StrokesPlus Forum
Home | Profile | Register | Active Topics
Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Lua Scripts
 Tutorials
 Guide: Keystrokes and Timing
 New Topic  Reply to Topic
 Printer Friendly
Author  Topic Next Topic  

Rob

USA
2514 Posts

Posted - 09/30/2015 :  11:52:31  Show Profile  Visit Rob's Homepage  Reply with Quote
Sometimes a key combination like CTRL+N doesn't work as expected when you have an action script call such as acSendKeys("^n"), but the following works fine
acSendControlDown()
acDelay(20)
acSendKeys("n")
acDelay(20)
acSendControlUp()
The reason for this has to do with timing and the way a program is internally written. In the real world (you at the keyboard), it's pretty much impossible for you to press and hold the CTRL key and then press another key in 1 millisecond. In reality, there's at least going to be 50ms from the time you press the CTRL key until you are able to press the other key, then another 50ms until you are able to release the CTRL key.

The next part of the problem has to do with exactly how the target program's message loop is constructed. In a perfect world, when it detects the CTRL key, it would store some flag to indicate the CTRL is being pressed so a subsequent keystroke is recognized as CTRL+(something); but it's not a requirement, an app is free to behave however the developers make it. So, imagine a message loop where the program turns on some kind of flag when the CTRL key is pressed down, then when it sees another keystroke it noticed the control key is being held down and then decides how to process that key combination. In that instance, acSendKeys("^t") would work fine as the CTRL key down is sent first immediately followed by "T". However, if the program calls getkeystate() to see if CTRL is held down AFTER receiving "T", S+ send CTRL+T together instantaneously, too fast for it to check if the CTRL key is down because it's already been released. There are other ways the app's code could be written where it gets a keystroke, then checks the control key; let's say this processing takes 2ms, but since the keystroke was sent in together and immediately released, the app wouldn't recognize the control key.

Ideal program:
(first key received is CTRL, second key is "T")
keystroke_message received
If keystroke_message = CTRL Then
   set flag that CTRL is being pressed --first pass
Else If keystroke_message is "T" Then
   If CTRL flag is set Then
      execute as a key combination CTRL+T
   Else
      process letter "T"
Else
   process the letter pressed

Less than ideal program:
keystroke_message received
If call to getkeystate() reports that the CTRL key is currently being pressed Then
   execute as a key combination CTRL+T
Else
   process the letter "T"
In the case of the less than ideal program, the message sent by acSendKeys would happen so fast that by the time it checked to see if the CTRL was being pressed, it would no longer be. Where the ideal program immediately stores that the CTRL is in the pressed position so the "T" is immediately recognized as a CTRL+T.

Basically, you have to think about it in terms of the real world. Sometimes programs are written and tested against actual people pressing keys, which is nowhere near as lightning-fast as a program (like S+) can send them. In a perfect world, people would write programs perfectly, but many times something like this just is never considered.

chandisciple

75 Posts

Posted - 11/12/2015 :  22:08:12  Show Profile  Reply with Quote
Very informative and helpful rob .

Thanks
Go to Top of Page
   Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
StrokesPlus Forum © 2011-2018 Rob Yapchanyk Go To Top Of Page
Snitz Forums 2000