MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey-v1.0 / ScriptGetKeyState

Method ScriptGetKeyState

Source/script2.cpp:7681–7744  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7679
7680
7681bool Line::ScriptGetKeyState(vk_type aVK, KeyStateTypes aKeyStateType)
7682// Returns true if "down", false if "up".
7683{
7684 if (!aVK) // Assume "up" if indeterminate.
7685 return false;
7686
7687 switch (aKeyStateType)
7688 {
7689 case KEYSTATE_TOGGLE: // Whether a toggleable key such as CapsLock is currently turned on.
7690 // Under Win9x, at least certain versions and for certain hardware, this
7691 // doesn't seem to be always accurate, especially when the key has just
7692 // been toggled and the user hasn't pressed any other key since then.
7693 // I tried using GetKeyboardState() instead, but it produces the same
7694 // result. Therefore, I've documented this as a limitation in the help file.
7695 // In addition, this was attempted but it didn't seem to help:
7696 //if (g_os.IsWin9x())
7697 //{
7698 // DWORD my_thread = GetCurrentThreadId();
7699 // DWORD fore_thread = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
7700 // bool is_attached_my_to_fore = false;
7701 // if (fore_thread && fore_thread != my_thread)
7702 // is_attached_my_to_fore = AttachThreadInput(my_thread, fore_thread, TRUE) != 0;
7703 // output_var->Assign(IsKeyToggledOn(aVK) ? "D" : "U");
7704 // if (is_attached_my_to_fore)
7705 // AttachThreadInput(my_thread, fore_thread, FALSE);
7706 // return OK;
7707 //}
7708 //else
7709 return IsKeyToggledOn(aVK);
7710 case KEYSTATE_PHYSICAL: // Physical state of key.
7711 if (VK_IS_MOUSE(aVK)) // mouse button
7712 {
7713 if (g_MouseHook) // mouse hook is installed, so use it's tracking of physical state.
7714 return g_PhysicalKeyState[aVK] & STATE_DOWN;
7715 else // Even for Win9x/NT, it seems slightly better to call this rather than IsKeyDown9xNT():
7716 return IsPhysicallyDown(aVK);
7717 }
7718 else // keyboard
7719 {
7720 if (g_KeybdHook)
7721 {
7722 // Since the hook is installed, use its value rather than that from
7723 // GetAsyncKeyState(), which doesn't seem to return the physical state
7724 // as expected/advertised, least under WinXP.
7725 // But first, correct the hook modifier state if it needs it. See comments
7726 // in GetModifierLRState() for why this is needed:
7727 if (KeyToModifiersLR(aVK)) // It's a modifier.
7728 GetModifierLRState(true); // Correct hook's physical state if needed.
7729 return g_PhysicalKeyState[aVK] & STATE_DOWN;
7730 }
7731 else // Even for Win9x/NT, it seems slightly better to call this rather than IsKeyDown9xNT():
7732 return IsPhysicallyDown(aVK);
7733 }
7734 } // switch()
7735
7736 // Otherwise, use the default state-type: KEYSTATE_LOGICAL
7737 if (g_os.IsWin9x() || g_os.IsWinNT4())
7738 return IsKeyDown9xNT(aVK); // This seems more likely to be reliable.

Callers

nothing calls this directly

Calls 4

KeyToModifiersLRFunction · 0.85
GetModifierLRStateFunction · 0.85
IsWin9xMethod · 0.80
IsWinNT4Method · 0.80

Tested by

no test coverage detected