Jump to content


Photo

Send keys at the same time, not in sequence.


  • Please log in to reply
5 replies to this topic

#1 Eedis

Eedis
  • Members
  • 1591 posts

Posted 21 December 2009 - 07:43 AM

I was just curious if you could send two keys simultaneously. I realize that using something like SetKeyDelay, SetBatchLines, and Send command could pretty much make it send both keys simultaneously, but there still is that very very very small increment of time between the two keys. I was wondering if it was theoretically possible to send two keys at the same time?

I've noticed that when you have two different scripts, when one has
^a::send, 1
and the other one is
^a::send, 2
when you press ^a it sends the number in which ever script comes last in alphabetical order.

I named the first script 'test.ahk' and the second was 'test2.ahk' and when I pressed ^a it sent 2. So I changed 'test.ahk' to 'test3.ahk' so that it'd come AFTER 'test2.ahk' alphabetically and it sent 1 instead.

#2 Lexikos

Lexikos
  • Administrators
  • 8856 posts

Posted 21 December 2009 - 07:57 AM

I was just curious if you could send two keys simultaneously.

No. What are you trying to do?
SendInput {1 down}{2 down}{1 up}{2 up}
...might be closer to what you want.

... it sends the letter in which ever script comes last in alphabetical order.

It depends on the order in which you load them, nothing to do with filenames.
If you use the ~ modifier on both hotkeys, both will work (as will the key-combination's original function).

#3 Eedis

Eedis
  • Members
  • 1591 posts

Posted 21 December 2009 - 08:03 AM

It depends on the order in which you load them, nothing to do with filenames..

No that is not true because I tested that theory. I ran them both in different orders as well and the rules I said above still apply.

No. What are you trying to do?

I'm not really wanting to do it for any specific reason, I was just curious if you could do it in the first place.

SendInput {1 down}{2 down}{1 up}{2 up}
...might be closer to what you want.

No, this won't work. It may seem like they're both sent at the same time, but 1 will ALWAYS be sent first. Even with the nanoseconds in between each push, they were not pushed at the same time.

If you use the ~ modifier on both hotkeys, both will work (as will the key-combination's original function).

It'll still send 1 first even though the time between each press is astronomically small.

I don't think you're fully understanding the question.

#4 Lexikos

Lexikos
  • Administrators
  • 8856 posts

Posted 21 December 2009 - 10:40 AM

It depends on the order in which you load them, nothing to do with filenames..

No that is not true because I tested that theory.

As did I (again just now) with exactly the result I expected: run 1.ahk first and 2.ahk takes precedence; conversely, run 2.ahk first and 1.ahk takes precedence. There is good reason for this behaviour: When the first script runs, it uses RegisterHotkey. When the second script runs, RegisterHotkey fails (as the hotkey is already registered by the other script) and the script must resort to the keyboard hook, which takes precedence. If both scripts use the keyboard hook (such as when RegisterHotkey fails both times, or ~ or #UseHook are in use), the script which loads last takes precedence as each newly registered keyboard hook is inserted at the beginning of the chain.

I've not seen anything to indicate a script's filename could or would affect the order of precedence of hotkeys.

No, this won't work. It may seem like they're both sent at the same time, but 1 will ALWAYS be sent first.

One key will always be sent (or received/processed) before the other, which is why I answered "No".

It'll still send 1 first even though the time between each press is astronomically small.

My testing has shown that isn't necessarily true. Although the keyboard hooks are invoked in a specific order and necessarily cannot execute simultaneously, that isn't true of hotkey subroutines (the script part of the hotkey) running in separate processes. I believe the chain of events for two scripts running ~a or similar hotkey is as follows:
[*:o15bf784]You press the key.
[*:o15bf784]Keyboard hook A recognizes the hotkey and fires a message to script A's main thread. It then continues to call the next keyboard hook.
[*:o15bf784]Keyboard hook B recognizes the hotkey and fires a message to script B's main thread. It then returns, allowing Windows to queue the original keyboard message in the target window's message queue.
[*:o15bf784]At some point (possibly but not necessarily during the above), script A's main thread receives its hotkey message and begins running the hotkey subroutine.
[*:o15bf784]As above for script B.
[*:o15bf784]Depending on how Windows schedules processor time for the two processes, script A or script B may finish first. (Understanding this requires some understanding of multithreading.)It's intentionally vague. On a single-core/single-processor system, only one process can be running at any instant. I think a typical timeslice is around 10ms, which may be more than enough to process the hotkey and send a keystroke. (In other words, the order might appear consistent on those systems.) However, on my dual-core system, the scripts presumably are able to run simultaneously, as indicated by the seemingly random order that the characters appear in the target window. I used the following scripts in this test:
~a::send 1
~a::send 2
After repeatedly tapping a, I had this:
a21a12a21a12a12a12a21a21a12a21a12a21a21a21a21a12a12a12a21a12a12a21a12a12

I don't think you're fully understanding the question.

In my previous post, only the first word was strictly in answer to your question.

#5 Guests

  • Guests

Posted 08 September 2012 - 03:17 PM

*w::
{
Send {w down}{a down}
While GetKeyState("w","P")
{
; do nothing
}
Send {w up}{a up}
}

#6 Eedis

Eedis
  • Members
  • 1591 posts

Posted 08 September 2012 - 07:09 PM

This post just got revived from the deepest levels of limbo.