Skip to content

Instantly share code, notes, and snippets.

@imakin
Created November 8, 2016 08:43
Show Gist options
  • Save imakin/76152c68f1b6ef8f208a02b88aea91d3 to your computer and use it in GitHub Desktop.
Save imakin/76152c68f1b6ef8f208a02b88aea91d3 to your computer and use it in GitHub Desktop.
from ctypes import *
import time
MOUSEEVENTF_LEFTDOWN = 2
MOUSEEVENTF_LEFTUP = 4
KEYCODE_LBUTTON = 0x01
KEYCODE_RBUTTON = 0x02
KEYCODE_MBUTTON = 0x04
KEYCODE_0 = 0x30
KEYCODE_A = 0x41
KEYCODE_ALT = 0xA4
KEYCODE_F1 = 0x70
class Point(Structure):
""" struct """
_fields_ = [("x", c_ulong), ("y", c_ulong)]
def mouse_click(x,y):
windll.user32.SetCursorPos(x,y)
windll.user32.mouse_event(MOUSEEVENTF_LEFTDOWN, 0,0,0,0)
windll.user32.mouse_event(MOUSEEVENTF_LEFTUP, 0,0,0,0)
def mouse_pos_get():
pos = Point()
windll.user32.GetCursorPos(byref(pos))
return pos
def mouse_pos_set(x,y):
windll.user32.SetCursorPos(x,y)
class HotKeyManager(object):
hotkeys = []
delay = 0.05
def __init__(self):
pass
def add(self, letter_key, function_pointer):
"""
@param letter_key the hotkey letter will be ALT+(letter_key)
@param function_pointer the callback function to be executed
"""
letter_key = ord(letter_key.upper()[0])
if (letter_key>=ord('A') and letter_key<=ord('Z')):
self.hotkeys.append({"key":letter_key, "f":function_pointer})
def check(self):
for hotkey in self.hotkeys:
if (
(windll.user32.GetAsyncKeyState(KEYCODE_ALT))!=0 and
(windll.user32.GetAsyncKeyState(hotkey["key"]))!=0
):
hotkey["f"]()
while (
(windll.user32.GetAsyncKeyState(KEYCODE_ALT))!=0 and
(windll.user32.GetAsyncKeyState(hotkey["key"]))!=0
):pass
def start(self):
while True:
self.check()
def test(self):
while True:
for hotkey in self.hotkeys:
print(windll.user32.GetAsyncKeyState(KEYCODE_ALT),
windll.user32.GetAsyncKeyState(hotkey["key"]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment