Skip to content

Commit e726343

Browse files
authored
Merge pull request #157 from Integration-Automation/dev
Dev
2 parents fd12edb + 556cf51 commit e726343

File tree

17 files changed

+226
-65
lines changed

17 files changed

+226
-65
lines changed

.idea/workspace.xml

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

stable.toml renamed to dev.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Rename to build stable version
2-
# This is stable version
1+
# Rename to build dev version
2+
# This is dev version
33
[build-system]
4-
requires = ["setuptools>=61.0"]
4+
requires = ["setuptools"]
55
build-backend = "setuptools.build_meta"
66

77
[project]
8-
name = "je_auto_control"
9-
version = "0.0.171"
8+
name = "je_auto_control_dev"
9+
version = "0.0.122"
1010
authors = [
1111
{ name = "JE-Chen", email = "[email protected]" },
1212
]
@@ -43,4 +43,4 @@ content-type = "text/markdown"
4343
find = { namespaces = false }
4444

4545
[project.optional-dependencies]
46-
gui = ["PySide6==6.9.1", "qt-material"]
46+
gui = ["PySide6==6.9.2", "qt-material"]

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/utils/shell_process/shell_exec.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import subprocess
44
import sys
55
from threading import Thread
6+
from typing import Union
67

78
from je_auto_control.utils.logging.loggin_instance import autocontrol_logger
89

@@ -21,13 +22,13 @@ def __init__(
2122
self.read_program_error_output_from_thread = None
2223
self.read_program_output_from_thread = None
2324
self.still_run_shell: bool = True
24-
self.process = None
25-
self.run_output_queue: queue = queue.Queue()
26-
self.run_error_queue: queue = queue.Queue()
25+
self.process: Union[subprocess.Popen, None] = None
26+
self.run_output_queue: queue.Queue = queue.Queue()
27+
self.run_error_queue: queue.Queue = queue.Queue()
2728
self.program_encoding: str = shell_encoding
2829
self.program_buffer: int = program_buffer
2930

30-
def exec_shell(self, shell_command: [str, list]) -> None:
31+
def exec_shell(self, shell_command: Union[str, list]) -> None:
3132
"""
3233
:param shell_command: shell command will run
3334
:return: if error return result and True else return result and False
@@ -115,16 +116,16 @@ def print_and_clear_queue(self) -> None:
115116

116117
def read_program_output_from_process(self) -> None:
117118
while self.still_run_shell:
118-
program_output_data = self.process.stdout.raw.read(
119+
program_output_data = self.process.stdout.readline(
119120
self.program_buffer) \
120-
.decode(self.program_encoding)
121+
.decode(self.program_encoding, "replace")
121122
self.run_output_queue.put_nowait(program_output_data)
122123

123124
def read_program_error_output_from_process(self) -> None:
124125
while self.still_run_shell:
125-
program_error_output_data = self.process.stderr.raw.read(
126+
program_error_output_data = self.process.stderr.readline(
126127
self.program_buffer) \
127-
.decode(self.program_encoding)
128+
.decode(self.program_encoding, "replace")
128129
self.run_error_queue.put_nowait(program_error_output_data)
129130

130131

je_auto_control/windows/core/utils/win32_keypress_check.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
from typing import Union
23

34
from je_auto_control.utils.exception.exception_tags import windows_import_error
45
from je_auto_control.utils.exception.exceptions import AutoControlException
@@ -9,11 +10,11 @@
910
import ctypes
1011

1112

12-
def check_key_is_press(keycode: [int, str]) -> bool:
13+
def check_key_is_press(keycode: Union[int, str]) -> bool:
1314
if isinstance(keycode, int):
1415
temp: int = ctypes.windll.user32.GetAsyncKeyState(keycode)
1516
else:
1617
temp = ctypes.windll.user32.GetAsyncKeyState(ord(keycode))
17-
if temp > 1:
18+
if temp != 0:
1819
return True
1920
return False

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)

0 commit comments

Comments
 (0)