Created
October 22, 2018 11:24
-
-
Save rikkimax/c197ab876606dc14409c41b0a283ae4d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum AllKeyModifiers = [KeyModifiers.LAlt, KeyModifiers.RAlt, KeyModifiers.LControl, KeyModifiers.RControl, | |
KeyModifiers.LShift, KeyModifiers.RShift, KeyModifiers.Capslock, KeyModifiers.Numlock, | |
KeyModifiers.LSuper, KeyModifiers.RSuper]; | |
enum AllVirtualKeyModifiers = [VK_LMENU, VK_RMENU, VK_LCONTROL, VK_RCONTROL, VK_LSHIFT, | |
VK_RSHIFT, VK_CAPITAL, VK_NUMLOCK, VK_LWIN, VK_RWIN]; | |
uint addKeyModifiersStart(INPUT[] inputs, ushort modifiersToDown, ushort modifiersToUp, ushort currentState) { | |
uint count; | |
// we should only press a key down if it already isn't pressed | |
// nor should we unpress a key if it is already not pressed | |
foreach(i, KM; AllKeyModifiers) { | |
if ((modifiersToDown & KM) == KM && (currentState & KM) == 0) | |
inputs[count++].ki.wVk = AllVirtualKeyModifiers[i]; | |
else if ((modifiersToUp & KM) == KM && (currentState & KM) != 0) { | |
inputs[count].ki.wVk = AllVirtualKeyModifiers[i]; | |
inputs[count++].ki.dwFlags = KEYEVENTF_KEYUP; | |
} | |
} | |
return count; | |
} | |
uint addKeyModifiersEnd(INPUT[] inputs, ushort modifiersToDown, ushort modifiersToUp) { | |
uint count; | |
// we should only unpress a key if we pressed it | |
// nor should we press a key if it is already not pressed | |
foreach(i, KM; AllKeyModifiers) { | |
if ((modifiersToUp & KM) == KM && (currentState & KM) != 0) | |
inputs[count++].ki.wVk = AllVirtualKeyModifiers[i]; | |
else if ((modifiersToDown & KM) == KM && (currentState & KM) == 0) { | |
inputs[count].ki.wVk = AllVirtualKeyModifiers[i]; | |
inputs[count++].ki.dwFlags = KEYEVENTF_KEYUP; | |
} | |
} | |
return count; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uint setKeyModifiersStart(INPUT[] inputs, ushort modifiers, ushort inverseModifiers) { | |
uint count; | |
if ((modifiers & KeyModifiers.LAlt) == KeyModifiers.LAlt) | |
inputs[count++].ki.wVk = VK_LMENU; | |
else if ((inverseModifiers & KeyModifiers.LAlt) == KeyModifiers.LAlt) { | |
inputs[count].ki.wVk = VK_LMENU; | |
inputs[count++].ki.dwFlags = KEYEVENTF_KEYUP; | |
} | |
if ((modifiers & KeyModifiers.RAlt) == KeyModifiers.RAlt) | |
inputs[count++].ki.wVk = VK_RMENU; | |
else if ((inverseModifiers & KeyModifiers.RAlt) == KeyModifiers.RAlt) { | |
inputs[count].ki.wVk = VK_RMENU; | |
inputs[count++].ki.dwFlags = KEYEVENTF_KEYUP; | |
} | |
if ((modifiers & KeyModifiers.LControl) == KeyModifiers.LControl) | |
inputs[count++].ki.wVk = VK_LCONTROL; | |
else if ((inverseModifiers & KeyModifiers.LControl) == KeyModifiers.LControl) { | |
inputs[count].ki.wVk = VK_LCONTROL; | |
inputs[count++].ki.dwFlags = KEYEVENTF_KEYUP; | |
} | |
if ((modifiers & KeyModifiers.RControl) == KeyModifiers.RControl) | |
inputs[count++].ki.wVk = VK_RCONTROL; | |
else if ((inverseModifiers & KeyModifiers.RControl) == KeyModifiers.RControl) { | |
inputs[count].ki.wVk = VK_RCONTROL; | |
inputs[count++].ki.dwFlags = KEYEVENTF_KEYUP; | |
} | |
if ((modifiers & KeyModifiers.LShift) == KeyModifiers.LShift) | |
inputs[count++].ki.wVk = VK_LSHIFT; | |
else if ((inverseModifiers & KeyModifiers.LShift) == KeyModifiers.LShift) { | |
inputs[count].ki.wVk = VK_LSHIFT; | |
inputs[count++].ki.dwFlags = KEYEVENTF_KEYUP; | |
} | |
if ((modifiers & KeyModifiers.RShift) == KeyModifiers.RShift) | |
inputs[count++].ki.wVk = VK_RSHIFT; | |
else if ((inverseModifiers & KeyModifiers.RShift) == KeyModifiers.RShift) { | |
inputs[count].ki.wVk = VK_RSHIFT; | |
inputs[count++].ki.dwFlags = KEYEVENTF_KEYUP; | |
} | |
if ((modifiers & KeyModifiers.Capslock) == KeyModifiers.Capslock) | |
inputs[count++].ki.wVk = VK_CAPITAL; | |
else if ((inverseModifiers & KeyModifiers.Capslock) == KeyModifiers.Capslock) { | |
inputs[count].ki.wVk = VK_CAPITAL; | |
inputs[count++].ki.dwFlags = KEYEVENTF_KEYUP; | |
} | |
if ((modifiers & KeyModifiers.Numlock) == KeyModifiers.Numlock) | |
inputs[count++].ki.wVk = VK_NUMLOCK; | |
else if ((inverseModifiers & KeyModifiers.Numlock) == KeyModifiers.Numlock) { | |
inputs[count].ki.wVk = VK_NUMLOCK; | |
inputs[count++].ki.dwFlags = KEYEVENTF_KEYUP; | |
} | |
if ((modifiers & KeyModifiers.LSuper) == KeyModifiers.LSuper) | |
inputs[count++].ki.wVk = VK_LWIN; | |
else if ((inverseModifiers & KeyModifiers.LSuper) == KeyModifiers.LSuper) { | |
inputs[count].ki.wVk = VK_LWIN; | |
inputs[count++].ki.dwFlags = KEYEVENTF_KEYUP; | |
} | |
if ((modifiers & KeyModifiers.RSuper) == KeyModifiers.RSuper) | |
inputs[count++].ki.wVk = VK_RWIN; | |
else if ((inverseModifiers & KeyModifiers.RSuper) == KeyModifiers.RSuper) { | |
inputs[count].ki.wVk = VK_RWIN; | |
inputs[count++].ki.dwFlags = KEYEVENTF_KEYUP; | |
} | |
return count; | |
} | |
uint setKeyModifiersEnd(INPUT[] inputs, ushort modifiers, ushort inverseModifiers) { | |
uint count; | |
if ((inverseModifiers & KeyModifiers.LAlt) == KeyModifiers.LAlt && HIWORD(GetKeyState(VK_LMENU)) == 0) | |
inputs[count++].ki.wVk = VK_LMENU; | |
else if ((modifiers & KeyModifiers.LAlt) == KeyModifiers.LAlt) { | |
inputs[count].ki.wVk = VK_LMENU; | |
inputs[count++].ki.dwFlags = KEYEVENTF_KEYUP; | |
} | |
if ((inverseModifiers & KeyModifiers.RAlt) == KeyModifiers.RAlt && HIWORD(GetKeyState(VK_RMENU)) == 0) | |
inputs[count++].ki.wVk = VK_RMENU; | |
else if ((modifiers & KeyModifiers.RAlt) == KeyModifiers.RAlt && HIWORD(GetKeyState(VK_RMENU)) != 0) { | |
inputs[count].ki.wVk = VK_RMENU; | |
inputs[count++].ki.dwFlags = KEYEVENTF_KEYUP; | |
} | |
if ((inverseModifiers & KeyModifiers.LControl) == KeyModifiers.LControl && HIWORD(GetKeyState(VK_LCONTROL)) == 0) | |
inputs[count++].ki.wVk = VK_LCONTROL; | |
else if ((modifiers & KeyModifiers.LControl) == KeyModifiers.LControl && HIWORD(GetKeyState(VK_LCONTROL)) != 0) { | |
inputs[count].ki.wVk = VK_LCONTROL; | |
inputs[count++].ki.dwFlags = KEYEVENTF_KEYUP; | |
} | |
if ((inverseModifiers & KeyModifiers.RControl) == KeyModifiers.RControl && HIWORD(GetKeyState(VK_RCONTROL)) == 0) | |
inputs[count++].ki.wVk = VK_RCONTROL; | |
else if ((modifiers & KeyModifiers.RControl) == KeyModifiers.RControl && HIWORD(GetKeyState(VK_RCONTROL)) != 0) { | |
inputs[count].ki.wVk = VK_RCONTROL; | |
inputs[count++].ki.dwFlags = KEYEVENTF_KEYUP; | |
} | |
if ((inverseModifiers & KeyModifiers.LShift) == KeyModifiers.LShift && HIWORD(GetKeyState(VK_LSHIFT)) == 0) | |
inputs[count++].ki.wVk = VK_LSHIFT; | |
else if ((modifiers & KeyModifiers.LShift) == KeyModifiers.LShift && HIWORD(GetKeyState(VK_LSHIFT)) != 0) { | |
inputs[count].ki.wVk = VK_LSHIFT; | |
inputs[count++].ki.dwFlags = KEYEVENTF_KEYUP; | |
} | |
if ((inverseModifiers & KeyModifiers.RShift) == KeyModifiers.RShift && HIWORD(GetKeyState(VK_RSHIFT)) == 0) | |
inputs[count++].ki.wVk = VK_RSHIFT; | |
else if ((modifiers & KeyModifiers.RShift) == KeyModifiers.RShift && HIWORD(GetKeyState(VK_RSHIFT)) != 0) { | |
inputs[count].ki.wVk = VK_RSHIFT; | |
inputs[count++].ki.dwFlags = KEYEVENTF_KEYUP; | |
} | |
if ((inverseModifiers & KeyModifiers.Capslock) == KeyModifiers.Capslock && HIWORD(GetKeyState(VK_CAPITAL)) == 0) | |
inputs[count++].ki.wVk = VK_CAPITAL; | |
else if ((modifiers & KeyModifiers.Capslock) == KeyModifiers.Capslock && HIWORD(GetKeyState(VK_CAPITAL)) != 0) { | |
inputs[count].ki.wVk = VK_CAPITAL; | |
inputs[count++].ki.dwFlags = KEYEVENTF_KEYUP; | |
} | |
if ((inverseModifiers & KeyModifiers.Numlock) == KeyModifiers.Numlock && HIWORD(GetKeyState(VK_NUMLOCK)) == 0) | |
inputs[count++].ki.wVk = VK_NUMLOCK; | |
else if ((modifiers & KeyModifiers.Numlock) == KeyModifiers.Numlock && HIWORD(GetKeyState(VK_NUMLOCK)) != 0) { | |
inputs[count].ki.wVk = VK_NUMLOCK; | |
inputs[count++].ki.dwFlags = KEYEVENTF_KEYUP; | |
} | |
if ((inverseModifiers & KeyModifiers.LSuper) == KeyModifiers.LSuper && HIWORD(GetKeyState(VK_LWIN)) == 0) | |
inputs[count++].ki.wVk = VK_LWIN; | |
else if ((modifiers & KeyModifiers.LSuper) == KeyModifiers.LSuper && HIWORD(GetKeyState(VK_LWIN)) != 0) { | |
inputs[count].ki.wVk = VK_LWIN; | |
inputs[count++].ki.dwFlags = KEYEVENTF_KEYUP; | |
} | |
if ((inverseModifiers & KeyModifiers.RSuper) == KeyModifiers.RSuper && HIWORD(GetKeyState(VK_RWIN)) == 0) | |
inputs[count++].ki.wVk = VK_RWIN; | |
else if ((modifiers & KeyModifiers.RSuper) == KeyModifiers.RSuper && HIWORD(GetKeyState(VK_RWIN)) != 0) { | |
inputs[count].ki.wVk = VK_RWIN; | |
inputs[count++].ki.dwFlags = KEYEVENTF_KEYUP; | |
} | |
return count; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment