MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / ScriptGetKeyState

Function ScriptGetKeyState

source/script2.cpp:17310–17354  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17308
17309
17310bool ScriptGetKeyState(vk_type aVK, KeyStateTypes aKeyStateType)
17311// Returns true if "down", false if "up".
17312{
17313 if (!aVK) // Assume "up" if indeterminate.
17314 return false;
17315
17316 switch (aKeyStateType)
17317 {
17318 case KEYSTATE_TOGGLE: // Whether a toggleable key such as CapsLock is currently turned on.
17319 return IsKeyToggledOn(aVK); // This also works for non-"lock" keys, but in that case the toggle state can be out of sync with other processes/threads.
17320 case KEYSTATE_PHYSICAL: // Physical state of key.
17321 if (IsMouseVK(aVK)) // mouse button
17322 {
17323 if (g_MouseHook) // mouse hook is installed, so use it's tracking of physical state.
17324 return g_PhysicalKeyState[aVK] & STATE_DOWN;
17325 else
17326 return IsKeyDownAsync(aVK);
17327 }
17328 else // keyboard
17329 {
17330 if (g_KeybdHook)
17331 {
17332 // Since the hook is installed, use its value rather than that from
17333 // GetAsyncKeyState(), which doesn't seem to return the physical state.
17334 // But first, correct the hook modifier state if it needs it. See comments
17335 // in GetModifierLRState() for why this is needed:
17336 if (KeyToModifiersLR(aVK)) // It's a modifier.
17337 GetModifierLRState(true); // Correct hook's physical state if needed.
17338 return g_PhysicalKeyState[aVK] & STATE_DOWN;
17339 }
17340 else
17341 return IsKeyDownAsync(aVK);
17342 }
17343 } // switch()
17344
17345 // Otherwise, use the default state-type: KEYSTATE_LOGICAL
17346 // On XP/2K at least, a key can be physically down even if it isn't logically down,
17347 // which is why the below specifically calls IsKeyDown() rather than some more
17348 // comprehensive method such as consulting the physical key state as tracked by the hook:
17349 // v1.0.42.01: For backward compatibility, the following hasn't been changed to IsKeyDownAsync().
17350 // One example is the journal playback hook: when a window owned by the script receives
17351 // such a keystroke, only GetKeyState() can detect the changed state of the key, not GetAsyncKeyState().
17352 // A new mode can be added to KeyWait & GetKeyState if Async is ever explicitly needed.
17353 return IsKeyDown(aVK);
17354}
17355
17356
17357

Callers 1

BIF_DECLFunction · 0.85

Calls 3

IsMouseVKFunction · 0.85
KeyToModifiersLRFunction · 0.85
GetModifierLRStateFunction · 0.85

Tested by

no test coverage detected