Like the title suggest it's about the win32api Virtual key codes
see : https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
I need to convert a letter in an entry into it's virutal key code. If the entry value it's A we need to convert it as 0x41 if it's B it will be 0x42 if it's C 0x43 etc. And it can also work with Shift,Ctrl or Alt (that's not only with letters). My problem is that I don't know how to get their virtual key code based on the Key in the entry.
Right here for example you have an entry with value U we should obtain the virtual key code of U that is => 0x55
So when we click on the key, GetKeyState() of win32api will detect it (because it will be in the form 0x0...) and a click will be made with the help of pyautogui.
It is absolutely necessary to convert the value of the input into virtual key code in order to get GetKeyState to work with
EDIT :
if win32api.GetKeyState(win32api.VkKeyScan('alt')) < 0:
but it return =>
if win32api.GetKeyState(win32api.VkKeyScan('ctrl')) < 0: TypeError: must be a unicode string of length 1
If it only work with one letter that's not that good... That's not a problem if numpad numbers or other are the same for keycode but the key that are not specific letters must work ...
CodePudding user response:
Pasting from [SO]: How to convert values in Entry to win32api Virtual-Key Codes? (@CristiFati's answer) (for some reason you deleted that question).
If you want a char -> key code mapping you could:
Create one manually (using the list from [MS.Docs]: Virtual-Key Codes)
Use [MS.Docs]: VkKeyScanW function (winuser.h) (could also check [MS.Docs]: MapVirtualKeyW function (winuser.h) out)
What's important to note:
More than one key could produce same virtual key code (e.g. 1 and NumPad 1)
There are no combined key codes, so for one character (when Shift, Alt, Ctrl are also pressed) there will be more than 1
code00.py:
#!/usr/bin/env python
import sys
import win32api as wapi
import win32con as wcon
def vks(c):
ret = []
res = wapi.VkKeyScan(c)
ss = (res >> 8) & 0xFF
if ss & 0x01:
ret.append(wcon.VK_SHIFT)
if ss & 0x02:
ret.append(wcon.VK_CONTROL)
if ss & 0x04:
ret.append(wcon.VK_MENU)
ret.append(res & 0xFF)
return tuple(ret)
def main(*argv):
print("Sample key values:")
print(["0x{:02X} ({:d})".format(e, e) for e in (wcon.VK_SHIFT, wcon.VK_CONTROL, wcon.VK_MENU, ord("y"), ord("Y"))])
print("\nScan cdess:")
for c in (
"y",
"Y",
"\x79",
"\x59",
"\x19",
):
print(vks(c))
if __name__ == "__main__":
print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
64 if sys.maxsize > 0x100000000 else 32, sys.platform))
rc = main(*sys.argv[1:])
print("\nDone.\n")
sys.exit(rc)
Output:
[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q073840049]> "e:\Work\Dev\VEnvs\py_pc064_03.09_test0\Scripts\python.exe" ./code00.py Python 3.9.9 (tags/v3.9.9:ccb0e6a, Nov 15 2021, 18:08:50) [MSC v.1929 64 bit (AMD64)] 064bit on win32 Sample key values: ['0x10 (16)', '0x11 (17)', '0x12 (18)', '0x79 (121)', '0x59 (89)'] Scan cdess: (89,) (16, 89) (89,) (16, 89) (17, 89) Done.