Skip to content

Instantly share code, notes, and snippets.

@lyf-is-coding
Created July 27, 2022 08:36
Show Gist options
  • Save lyf-is-coding/191fa56b909a4985f134587e61b97e01 to your computer and use it in GitHub Desktop.
Save lyf-is-coding/191fa56b909a4985f134587e61b97e01 to your computer and use it in GitHub Desktop.
Python parse Windows Virtual-Key Codes
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