Skip to content

Commit 9b08383

Browse files
committed
Update dev version
Update dev version * Add sent event to background window * Add windows window manage
1 parent 1b8ec26 commit 9b08383

File tree

14 files changed

+163
-16
lines changed

14 files changed

+163
-16
lines changed

.idea/workspace.xml

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

je_auto_control/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
from je_auto_control.wrapper.auto_control_keyboard import release_keyboard_key
8888
from je_auto_control.wrapper.auto_control_keyboard import type_keyboard
8989
from je_auto_control.wrapper.auto_control_keyboard import write
90+
from je_auto_control.wrapper.auto_control_keyboard import send_key_event_to_window
9091
# import mouse
9192
from je_auto_control.wrapper.auto_control_mouse import click_mouse
9293
from je_auto_control.wrapper.auto_control_mouse import get_mouse_position
@@ -96,6 +97,7 @@
9697
from je_auto_control.wrapper.auto_control_mouse import release_mouse
9798
from je_auto_control.wrapper.auto_control_mouse import set_mouse_position
9899
from je_auto_control.wrapper.auto_control_mouse import special_mouse_keys_table
100+
from je_auto_control.wrapper.auto_control_mouse import send_mouse_event_to_window
99101
# test_record
100102
from je_auto_control.wrapper.auto_control_record import record
101103
from je_auto_control.wrapper.auto_control_record import stop_record
@@ -104,6 +106,8 @@
104106
from je_auto_control.wrapper.auto_control_screen import screenshot
105107
# Recording
106108
from je_auto_control.utils.cv2_utils.video_recording import RecordingThread
109+
# Windows
110+
from je_auto_control.windows.window import windows_window_manage
107111

108112
__all__ = [
109113
"click_mouse", "mouse_keys_table", "get_mouse_position", "press_mouse", "release_mouse",
@@ -120,5 +124,5 @@
120124
"generate_html", "generate_html_report", "generate_json", "generate_json_report", "generate_xml",
121125
"generate_xml_report", "get_dir_files_as_list", "create_project_dir", "start_autocontrol_socket_server",
122126
"callback_executor", "package_manager", "get_special_table", "ShellManager", "default_shell_manager",
123-
"RecordingThread"
127+
"RecordingThread", "send_key_event_to_window", "send_mouse_event_to_window", "windows_window_manage"
124128
]

je_auto_control/linux_with_x11/keyboard/x11_linux_keyboard_control.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from je_auto_control.linux_with_x11.core.utils.x11_linux_display import display
1111
from Xlib.ext.xtest import fake_input
12-
from Xlib import X
12+
from Xlib import X, protocol
1313

1414

1515
def press_key(keycode: int) -> None:
@@ -28,3 +28,20 @@ def release_key(keycode: int) -> None:
2828
time.sleep(0.01)
2929
fake_input(display, X.KeyRelease, keycode)
3030
display.sync()
31+
32+
def send_key_event_to_window(window_id, keycode: int):
33+
window = display.create_resource_object('window', window_id)
34+
event = protocol.event.KeyPress(
35+
time=X.CurrentTime,
36+
root=display.screen().root,
37+
window=window,
38+
same_screen=1,
39+
child=X.NONE,
40+
root_x=0, root_y=0, event_x=0, event_y=0,
41+
state=0,
42+
detail=keycode
43+
)
44+
window.send_event(event, propagate=True)
45+
event.type = X.KeyRelease
46+
window.send_event(event, propagate=True)
47+
display.flush()

je_auto_control/linux_with_x11/mouse/x11_linux_mouse_control.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
if sys.platform not in ["linux", "linux2"]:
99
raise AutoControlException(linux_import_error)
1010

11-
from Xlib import X
11+
from Xlib import X, protocol
1212
from Xlib.ext.xtest import fake_input
1313

1414
from je_auto_control.linux_with_x11.core.utils.x11_linux_display import display
@@ -83,3 +83,23 @@ def scroll(scroll_value: int, scroll_direction: int) -> None:
8383
for i in range(scroll_value):
8484
click_mouse(scroll_direction)
8585
total = total + i
86+
87+
def send_mouse_event_to_window(window_id, mouse_keycode: int, x: int = None, y: int = None):
88+
window = display.create_resource_object('window', window_id)
89+
for ev_type in (X.ButtonPress, X.ButtonRelease):
90+
ev = protocol.event.ButtonPress(
91+
time=X.CurrentTime,
92+
root=display.screen().root,
93+
window=window,
94+
same_screen=1,
95+
child=X.NONE,
96+
root_x=x, root_y=y, event_x=x, event_y=y,
97+
state=0,
98+
detail=mouse_keycode
99+
)
100+
ev.type = ev_type
101+
window.send_event(ev, propagate=True)
102+
display.flush()
103+
104+
105+

je_auto_control/osx/pid/__init__.py

Whitespace-only changes.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import objc
2+
from Quartz import CGEventCreateKeyboardEvent, kCGEventKeyDown, kCGEventKeyUp
3+
from ApplicationServices import ProcessSerialNumber, GetProcessForPID
4+
from ctypes import cdll, c_void_p
5+
import subprocess
6+
7+
carbon = cdll.LoadLibrary('/System/Library/Frameworks/Carbon.framework/Carbon')
8+
9+
10+
def send_key_to_pid(pid, keycode):
11+
psn = ProcessSerialNumber()
12+
GetProcessForPID(pid, objc.byref(psn))
13+
event_down = CGEventCreateKeyboardEvent(None, keycode, True)
14+
event_up = CGEventCreateKeyboardEvent(None, keycode, False)
15+
carbon.CGEventPostToPSN(c_void_p(id(psn)), event_down)
16+
carbon.CGEventPostToPSN(c_void_p(id(psn)), event_up)
17+
18+
def get_pid_by_window_title(title: str):
19+
script = f'''
20+
set targetWindowName to "{title}"
21+
tell application "System Events"
22+
repeat with proc in processes
23+
repeat with win in windows of proc
24+
if name of win contains targetWindowName then
25+
return unix id of proc
26+
end if
27+
end repeat
28+
end repeat
29+
end tell
30+
'''
31+
try:
32+
pid_str = subprocess.check_output(
33+
["osascript", "-e", script],
34+
stderr=subprocess.DEVNULL
35+
).decode().strip()
36+
return int(pid_str) if pid_str else None
37+
except subprocess.CalledProcessError:
38+
return None

je_auto_control/windows/keyboard/win32_ctype_keyboard_control.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
if sys.platform not in ["win32", "cygwin", "msys"]:
77
raise AutoControlException(windows_import_error)
88

9-
from je_auto_control.windows.core.utils.win32_ctype_input import Input
9+
from je_auto_control.windows.core.utils.win32_ctype_input import Input, user32
1010
from je_auto_control.windows.core.utils.win32_ctype_input import Keyboard
1111
from je_auto_control.windows.core.utils.win32_ctype_input import KeyboardInput
1212
from je_auto_control.windows.core.utils.win32_ctype_input import SendInput
@@ -28,3 +28,10 @@ def release_key(keycode: int) -> None:
2828
"""
2929
keyboard = Input(type=Keyboard, ki=KeyboardInput(wVk=keycode, dwFlags=win32_EventF_KEYUP))
3030
SendInput(1, ctypes.byref(keyboard), ctypes.sizeof(keyboard))
31+
32+
def send_key_event_to_window(window: str, keycode: int):
33+
WM_KEYDOWN = 0x0100
34+
WM_KEYUP = 0x0101
35+
window = user32.FindWindowW(None, window)
36+
user32.PostMessageW(window, WM_KEYDOWN, keycode, 0)
37+
user32.PostMessageW(window, WM_KEYUP, keycode, 0)

je_auto_control/windows/message/window_message.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from je_auto_control.windows.core.utils.win32_ctype_input import user32
2-
from je_auto_control.windows.window.window_hwnd import FindWindowW
2+
from je_auto_control.windows.window.windows_window_manage import FindWindowW
33

44
PostMessageW = user32.PostMessageW
55
SendMessageW = user32.SendMessageW

je_auto_control/windows/mouse/win32_ctype_mouse_control.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
if sys.platform not in ["win32", "cygwin", "msys"]:
88
raise AutoControlException(windows_import_error)
99

10-
from je_auto_control.windows.core.utils.win32_ctype_input import Input
10+
from je_auto_control.windows.core.utils.win32_ctype_input import Input, user32
1111
from je_auto_control.windows.core.utils.win32_vk import win32_LEFTDOWN
1212
from je_auto_control.windows.core.utils.win32_vk import win32_LEFTUP
1313
from je_auto_control.windows.core.utils.win32_vk import win32_MIDDLEDOWN
@@ -50,7 +50,7 @@ def mouse_event(event, x: int, y: int, dwData: int = 0) -> None:
5050
ctypes.windll.user32.mouse_event(event, ctypes.c_long(converted_x), ctypes.c_long(converted_y), dwData, 0)
5151

5252

53-
def position() -> [Tuple[int, int], None]:
53+
def position() -> tuple[int, int] | None:
5454
"""
5555
get mouse position
5656
"""
@@ -102,10 +102,16 @@ def click_mouse(mouse_keycode: int, x: int = None, y: int = None) -> None:
102102
release_mouse(mouse_keycode)
103103

104104

105-
def scroll(scroll_value: int, x: int = None, y: int = None) -> None:
105+
def scroll(scroll_value: int, x: int = None, y: int = None) -> None:
106106
"""
107107
:param scroll_value scroll count
108108
:param x scroll x
109109
:param y scroll y
110110
"""
111111
mouse_event(win32_WHEEL, x, y, dwData=scroll_value)
112+
113+
114+
def send_mouse_event_to_window(window, mouse_keycode: int, x: int = None, y: int = None):
115+
lparam = (y << 16) | x
116+
user32.PostMessageW(window, mouse_keycode, 1, lparam)
117+
user32.PostMessageW(window, mouse_keycode, 0, lparam)

je_auto_control/windows/window/window_hwnd.py renamed to je_auto_control/windows/window/windows_window_manage.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,12 @@ def close_window(hwnd) -> bool:
3838

3939
def destroy_window(hwnd) -> bool:
4040
return DestroyWindow(hwnd)
41+
42+
43+
def set_foreground_window(hwnd) -> None:
44+
user32.SetForegroundWindow(hwnd)
45+
46+
def set_window_positon(hwnd, position: int) -> None:
47+
swp_no_size = 0x0001
48+
swp_no_move = 0x0002
49+
user32.SetWindowPos(hwnd, position, 0, 0, 0, 0, swp_no_move | swp_no_size)

0 commit comments

Comments
 (0)