Created
July 27, 2022 08:36
-
-
Save lyf-is-coding/191fa56b909a4985f134587e61b97e01 to your computer and use it in GitHub Desktop.
Python parse Windows Virtual-Key Codes
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
import re | |
input = open("vk.txt", 'r') | |
Lines = input.readlines() | |
output = open("virtual_key_code.h", 'w') | |
output.writelines("#pragma once\n\n") | |
output.writelines("#ifndef _VIRTUALKEYCODE_H_\n") | |
output.writelines("#define _VIRTUALKEYCODE_H_\n\n") | |
output.writelines("enum VK\n") | |
output.writelines("{\n") | |
for line in Lines: | |
res = re.findall(r"""(?<= \| )(?:[^`][^\| ]+)+|[^`]+(?=` )|(?<= )\w key""", line) | |
# CONTROL đã có LCONTROL và RCONTROL | |
# SHIFT ALT ... tương tự | |
if ("VK_CONTROL" == res[0] or "VK_SHIFT" == res[0] or "VK_ALT" == res[0]): | |
res[0] = res[0].replace("VK_", '') | |
output.writelines(" // " + res[0] + " = " + res[1] + ", // " + res[2] + '\n') | |
if ("VK_" in res[0]): | |
res[0] = res[0].replace("VK_", '') | |
output.writelines(" " + res[0] + " = " + res[1] + ", // " + res[2] + '\n') | |
if ("-" == res[0]): | |
output.writelines(" // " + res[1] + " " + res[2] + '\n') | |
if (len(res) == 2): | |
if (res[1] == "OEM specific"): | |
output.writelines(" // " + res[0] + " " + res[1] + '\n') | |
continue | |
tmp = res[1][:res[1].index(" ")] | |
if (tmp.isnumeric()): | |
output.writelines(" NUM" + tmp + " = " + res[0] + ", // " + res[1] + '\n') | |
else: | |
output.writelines(" " + tmp + " = " + res[0] + ", // " + res[1] + '\n') | |
output.writelines(" Last = -1,\n") | |
output.writelines("};\n\n") | |
output.writelines("#endif //_VIRTUALKEYCODE_H_") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment