You can wait for multiple keys to be pressed simultaneously in AutoHotkey by using the
GetKey function within a loop or by combining key names with the logical OR operator (
||) inside conditional statements. This approach relies on Virtual Key (VK) codes, which are the standard identifiers for most keyboard inputs, allowing you to detect complex combinations without resorting to Scan Code (SC) mappings that vary by hardware.
Understanding Virtual Keys vs. Scan Codes
When scripting keyboard interactions, it is crucial to distinguish between Virtual Key (VK) codes and Scan Code (SC) codes. VK codes are consistent across different keyboard layouts and hardware manufacturers, representing the logical key press (e.g., 'A', 'B', 'Enter'). In contrast, SC codes represent the physical location of the key on the keyboard matrix. For most automation tasks, especially those requiring compatibility across various machines, VK codes are the preferred method because they remain stable regardless of whether you are using a US QWERTY, UK AZERTY, or German QWERTZ layout.
The original problem often arises when users attempt to use scan codes for multi-key detection, only to find their scripts failing on different keyboards. By sticking to VK codes, you ensure that your script waits for the intended logical action rather than a specific physical switch position. This distinction simplifies the logic significantly, as you do not need to maintain a mapping table for every possible keyboard variant.
Using GetKey for Simultaneous Detection
The most robust way to wait for multiple keys is to use the
GetKey function. This function returns true if the specified key is currently held down. To check for multiple keys, you can chain these checks together. For example, if you want to wait until both the Shift key and the Z key are pressed, you would structure your condition to evaluate both states.
WaitForKeys() {
while (!GetKey("Shift") || !GetKey("z")) {
Sleep 10
}
return true
}
In this snippet, the loop continues until both conditions are met. The
Sleep 10 command prevents the script from consuming excessive CPU resources by yielding control to other processes for ten milliseconds during each iteration. This method is highly reliable because it directly queries the state of the operating system's input buffer for each specific key.
Combining Keys with Logical Operators
While the loop method is explicit, you can also achieve similar results by using logical operators within conditional blocks. AutoHotkey supports the logical OR (
||) and AND (
&&) operators, which allow you to create complex boolean expressions. When waiting for any one of several keys to be pressed, you use the OR operator. When waiting for all specified keys to be pressed, you use the AND operator.
if (GetKey("LButton") && GetKey("RButton")) {
MsgBox Both mouse buttons are pressed!
}
This syntax is cleaner for simple checks but can become verbose if you are checking for many different combinations. It is important to note that
GetKey checks the current state at the moment of evaluation. If you need to trigger an action only once when the combination occurs, you should wrap this check in a hotkey definition or a dedicated thread to avoid continuous triggering.
Handling Modifier Keys Specifically
Modifier keys like Shift, Control, Alt, and Win behave slightly differently in some contexts, but
GetKey handles them uniformly. You can specify the modifier by its name, such as "Shift", "Ctrl", "Alt", or "Win". For left and right variants, you can append L or R, such as "LShift" or "RControl". This specificity is useful if your automation requires distinguishing between left and right modifiers, although for most general purposes, the generic name suffices.
It is also worth noting that some applications may consume modifier keys before your script sees them. If your script fails to detect a key press, ensure that the target application is not intercepting the input. Using
#UseHook at the top of your script can help by forcing AutoHotkey to install a low-level keyboard hook, which increases the priority of your script's input handling.
Mastering Multiple Commands on One Line with AutoHotkey demonstrates how to streamline the actions taken after your key detection logic completes, ensuring your script remains efficient and readable.
Troubleshooting Common Issues
If your script does not respond to key presses, verify that the key names are spelled correctly and case-insensitively. AutoHotkey is generally case-insensitive for key names, but consistency helps readability. Additionally, ensure that no other script or system process is holding the key down. You can use the
KeyHistory built-in feature to see what keys the system has recently registered, which helps diagnose whether the input is reaching the OS level.
Another common issue is the timing of the check. If you are checking for a key combination in a tight loop without a
Sleep, your CPU usage will spike. Always include a small delay in your waiting loops. Furthermore, if you are working with non-standard keys or media keys, you might need to refer to the AutoHotkey documentation for specific virtual key codes, as some special keys do not have standard text names.
AutoHotkey Not Sending Commands: A Troubleshooting Guide for Tech Support provides additional context on ensuring your script's commands are executed correctly after the initial detection phase.
Frequently Asked Questions
Can I use scan codes instead of virtual keys?
Yes, you can use scan codes by prefixing the key name with
SC (e.g.,
SC021), but they are hardware-dependent and may not work across different keyboard models. Virtual keys are recommended for portability unless you have a specific reason to target a physical key location.
How do I detect three keys pressed at once?
You can extend the logical AND operator to include three conditions, such as
if (GetKey("Ctrl") && GetKey("Shift") && GetKey("z")). Ensure you add a
Sleep command in any loop used to monitor these states to prevent high CPU usage.
Why is my script missing key presses?
This often happens if another application is capturing the input or if the script lacks a low-level hook. Adding
#UseHook to the top of your script forces AutoHotkey to monitor input at a lower level, increasing reliability.
Does GetKey work for mouse buttons?
Yes,
GetKey works for mouse buttons as well. You can use "LButton", "MButton", or "RButton" to check the state of the left, middle, or right mouse buttons respectively.
What is the difference between Wait and GetKey?
Wait is typically used for hotkeys and pauses execution until a specific key is pressed.
GetKey is a function that returns a boolean value indicating the current state of a key, making it more flexible for custom logic loops and conditional checks.