Skip to content

Commit

Permalink
Merge pull request #1 from JE-Chen/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
JE-Chen committed Oct 8, 2021
2 parents eca07ab + 0178dff commit 5e42dae
Show file tree
Hide file tree
Showing 13 changed files with 262 additions and 57 deletions.
122 changes: 71 additions & 51 deletions .idea/workspace.xml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions je_auto_control/linux_with_x11/listener/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from je_auto_control.linux_with_x11.listener import *
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
if sys.platform not in ["linux", "linux2"]:
raise Exception("should be only loaded on linux")

import Xlib.threaded

from Xlib.display import Display
from Xlib import X
from Xlib.ext import record
from Xlib.protocol import rq

from je_auto_control.linux_with_x11.mouse.x11_linux_mouse_control import position

from threading import Thread

# get current display
Expand All @@ -26,6 +30,7 @@ def __init__(self, default_daemon=True):
self.setDaemon(default_daemon)
self.still_listener = True
self.event_keycode = 0
self.event_position = 0, 0

# two times because press and release
def check_is_press(self, keycode):
Expand All @@ -51,7 +56,10 @@ def run(self, reply):
while len(data) and self.still_listener:
event, data = rq.EventField(None).parse_binary_value(data, current_display.display, None, None)
# run two times because press and release event
self.event_keycode = event.detail
if event.detail != 0:
self.event_keycode = event.detail
self.event_position = event.root_x, event.root_y

except Exception:
raise Exception

Expand Down Expand Up @@ -124,3 +132,7 @@ def check_key_is_press(keycode):
"""
return xwindows_listener.check_is_press(keycode)


if __name__ == "__main__":
while True:
pass
1 change: 1 addition & 0 deletions je_auto_control/linux_with_x11/record/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from je_auto_control.linux_with_x11.record import *
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import ctypes
import sys

if sys.platform not in ["win32", "cygwin", "msys"]:
raise Exception("should be only loaded on windows")

import ctypes


def check_key_is_press(keycode):
if type(keycode) is int:
Expand Down
1 change: 1 addition & 0 deletions je_auto_control/windows/listener/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from je_auto_control.windows.listener import *
59 changes: 59 additions & 0 deletions je_auto_control/windows/listener/win32_keyboard_listener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from ctypes import *
from ctypes.wintypes import MSG

user32 = windll.user32
kernel32 = windll.kernel32

wm_keydown = 0x100


class Win32Listener:

def __init__(self, hook_event_code_int):
self.hooked = None
self.hook_event_code_int = hook_event_code_int

def set_win32_hook(self, point):
self.hooked = user32.SetWindowsHookExA(
self.hook_event_code_int,
point,
0,
0
)
if not self.hooked:
return False
return True

def remove_win32_hook_proc(self):
if self.hooked is None:
return
user32.UnhookWindowsHookEx(self.hooked)
self.hooked = None

def win32_hook_proc(self, code, w_param, l_param):
if w_param is not wm_keydown:
return user32.CallNextHookEx(self.hooked, code, w_param, l_param)
# int to hex
temp = hex(l_param[0] & 0xFFFFFFFF)
print("Hooked Key: " + temp)
print(int(temp, 16))
return user32.CallNextHookEx(self.hooked, code, w_param, l_param)

def get_function_pointer(self, function):
win_function = WINFUNCTYPE(c_int, c_int, c_int, POINTER(c_void_p))
return win_function(function)

def start_listener(self):
pointer = self.get_function_pointer(self.win32_hook_proc)
if self.set_win32_hook(pointer):
print("start listener")
else:
print("failed to start")
message = MSG()
user32.GetMessageA(byref(message), 0, 0, 0)


if __name__ == "__main__":
wh_keyboard_ll = 13
win32_listener = Win32Listener(wh_keyboard_ll)
win32_listener.start_listener()
52 changes: 52 additions & 0 deletions je_auto_control/windows/listener/win32_listener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from ctypes import *
from ctypes.wintypes import MSG

user32 = windll.user32
kernel32 = windll.kernel32


class Win32Listener:

def __init__(self, hook_event_code_int):
self.hooked = None
self.hook_event_code_int = hook_event_code_int

def set_win32_hook(self, point):
self.hooked = user32.SetWindowsHookExA(
self.hook_event_code_int,
point,
0,
0
)
if not self.hooked:
return False
return True

def remove_win32_hook_proc(self):
if self.hooked is None:
return
user32.UnhookWindowsHookEx(self.hooked)
self.hooked = None

def win32_hook_proc(self, code, w_param, l_param):
# extend this class to rewrite this hook proc
return user32.CallNextHookEx(self.hooked, code, w_param, l_param)

def get_function_pointer(self, function):
win_function = WINFUNCTYPE(c_int, c_int, c_int, POINTER(c_void_p))
return win_function(function)

def start_listener(self):
pointer = self.get_function_pointer(self.win32_hook_proc)
if self.set_win32_hook(pointer):
print("start listener")
else:
print("failed to start")
message = MSG()
user32.GetMessageA(byref(message), 0, 0, 0)


if __name__ == "__main__":
wh_keyboard_ll = 13
win32_listener = Win32Listener(wh_keyboard_ll)
win32_listener.start_listener()
58 changes: 58 additions & 0 deletions je_auto_control/windows/listener/win32_mouse_listener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from ctypes import *
from ctypes.wintypes import MSG

from je_auto_control.windows.mouse.win32_ctype_mouse_control import position

user32 = windll.user32
kernel32 = windll.kernel32

wm_mouse_key_code = [0x0201, 0x0204]


class Win32Listener:

def __init__(self, hook_event_code_int):
self.hooked = None
self.hook_event_code_int = hook_event_code_int

def set_win32_hook(self, point):
self.hooked = user32.SetWindowsHookExA(
self.hook_event_code_int,
point,
0,
0
)
if not self.hooked:
return False
return True

def remove_win32_hook_proc(self):
if self.hooked is None:
return
user32.UnhookWindowsHookEx(self.hooked)
self.hooked = None

def win32_hook_proc(self, code, w_param, l_param):
if w_param not in wm_mouse_key_code:
return user32.CallNextHookEx(self.hooked, code, w_param, l_param)
print(position())
return user32.CallNextHookEx(self.hooked, code, w_param, l_param)

def get_function_pointer(self, function):
win_function = WINFUNCTYPE(c_int, c_int, c_int, POINTER(c_void_p))
return win_function(function)

def start_listener(self):
pointer = self.get_function_pointer(self.win32_hook_proc)
if self.set_win32_hook(pointer):
print("start listener")
else:
print("failed to start")
message = MSG()
user32.GetMessageA(byref(message), 0, 0, 0)


if __name__ == "__main__":
wh_mouse_ll = 14
win32_listener = Win32Listener(wh_mouse_ll)
win32_listener.start_listener()
Empty file.
8 changes: 4 additions & 4 deletions je_auto_control/wrapper/platform_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
from je_auto_control.windows.core.utils.win32_vk import win32_keyY
from je_auto_control.windows.core.utils.win32_vk import win32_keyZ
from je_auto_control.windows.keyboard import win32_ctype_keyboard_control
from je_auto_control.windows.keyboard import win32_keyboard_listener
from je_auto_control.windows.keyboard import win32_keyboard_check
from je_auto_control.windows.mouse import win32_ctype_mouse_control
from je_auto_control.windows.mouse.win32_ctype_mouse_control import win32_mouse_left
from je_auto_control.windows.mouse.win32_ctype_mouse_control import win32_mouse_middle
Expand Down Expand Up @@ -474,7 +474,7 @@
from je_auto_control.linux_with_x11.mouse.x11_linux_mouse_control import x11_linux_scroll_direction_left
from je_auto_control.linux_with_x11.mouse.x11_linux_mouse_control import x11_linux_scroll_direction_right
from je_auto_control.linux_with_x11.keyboard import x11_linux_keyboard_control
from je_auto_control.linux_with_x11.keyboard import x11_linux_keyboard_listener
from je_auto_control.linux_with_x11.listener import x11_linux_listener
from je_auto_control.linux_with_x11.mouse import x11_linux_mouse_control
from je_auto_control.linux_with_x11.screen import x11_linux_screen

Expand Down Expand Up @@ -693,7 +693,7 @@
"mouse_x2": win32_mouse_x2
}
keyboard = win32_ctype_keyboard_control
keyboard_listener = win32_keyboard_listener
keyboard_listener = win32_keyboard_check
mouse = win32_ctype_mouse_control
screen = win32_screen

Expand Down Expand Up @@ -1054,7 +1054,7 @@
"scroll_right": x11_linux_scroll_direction_right
}
keyboard = x11_linux_keyboard_control
keyboard_listener = x11_linux_keyboard_listener
keyboard_listener = x11_linux_listener
mouse = x11_linux_mouse_control
screen = x11_linux_screen
if None in [keys_table, mouse_table, special_table, keyboard, mouse, screen]:
Expand Down

0 comments on commit 5e42dae

Please sign in to comment.