From 8369e584ca02335150aeef069243d37a3f88d3fe Mon Sep 17 00:00:00 2001 From: JE-Chen <33644111+JE-Chen@users.noreply.github.com> Date: Wed, 20 Dec 2023 18:54:02 +0800 Subject: [PATCH] Add post and send message to window Add post and send message to window --- .idea/workspace.xml | 20 ++------ .../core/utils/win32_keypress_check.py | 4 +- je_auto_control/windows/window/__init__.py | 0 je_auto_control/windows/window/window_hwnd.py | 47 +++++++++++++++++++ 4 files changed, 54 insertions(+), 17 deletions(-) create mode 100644 je_auto_control/windows/window/__init__.py create mode 100644 je_auto_control/windows/window/window_hwnd.py diff --git a/.idea/workspace.xml b/.idea/workspace.xml index cf42c7c..38086bd 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,19 +5,7 @@ - - - - - - - - - - - - @@ -528,6 +518,6 @@ - + \ No newline at end of file diff --git a/je_auto_control/windows/core/utils/win32_keypress_check.py b/je_auto_control/windows/core/utils/win32_keypress_check.py index 27f6895..09ff2b1 100644 --- a/je_auto_control/windows/core/utils/win32_keypress_check.py +++ b/je_auto_control/windows/core/utils/win32_keypress_check.py @@ -11,9 +11,9 @@ def check_key_is_press(keycode: [int, str]) -> bool: if isinstance(keycode, int): - temp: int = ctypes.windll.user32.GetKeyState(keycode) + temp: int = ctypes.windll.user32.GetAsyncKeyState(keycode) else: - temp = ctypes.windll.user32.GetKeyState(ord(keycode)) + temp = ctypes.windll.user32.GetAsyncKeyState(ord(keycode)) if temp > 1: return True return False diff --git a/je_auto_control/windows/window/__init__.py b/je_auto_control/windows/window/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/je_auto_control/windows/window/window_hwnd.py b/je_auto_control/windows/window/window_hwnd.py new file mode 100644 index 0000000..69399ac --- /dev/null +++ b/je_auto_control/windows/window/window_hwnd.py @@ -0,0 +1,47 @@ +from ctypes import WINFUNCTYPE, c_bool, c_int, POINTER, create_unicode_buffer +from typing import Union + +from je_auto_control.windows.core.utils.win32_ctype_input import user32 +from je_auto_control.windows.keyboard.win32_ctype_keyboard_control import press_key + +EnumWindows = user32.EnumWindows +EnumWindowsProc = WINFUNCTYPE(c_bool, POINTER(c_int), POINTER(c_int)) +GetWindowText = user32.GetWindowTextW +GetWindowTextLength = user32.GetWindowTextLengthW +IsWindowVisible = user32.IsWindowVisible +FindWindowW = user32.FindWindowW +PostMessageW = user32.PostMessageW +SendMessageW = user32.SendMessageW + + +def get_all_window_hwnd(): + window_info = [] + + def _foreach_window(hwnd, l_param): + if IsWindowVisible(hwnd): + length = GetWindowTextLength(hwnd) + buff = create_unicode_buffer(length + 1) + GetWindowText(hwnd, buff, length + 1) + window_info.append((hwnd, buff.value)) + return True + + EnumWindows(EnumWindowsProc(_foreach_window), 0) + return window_info + + +def get_one_window_hwnd(window_class: Union[None, str], window_name: Union[None, str]): + return FindWindowW(window_class, window_name) + + +def send_key_to_window(window_name: str, action_message: int, + key_code_1: int, key_code_2: int): + _hwnd = FindWindowW(window_name) + post_status = SendMessageW(_hwnd, action_message, key_code_1, key_code_2) + return _hwnd, post_status + + +def post_key_to_window(window_name: str, action_message: int, + key_code_1: int, key_code_2: int): + _hwnd = FindWindowW(window_name) + post_status = PostMessageW(_hwnd, action_message, key_code_1, key_code_2) + return _hwnd, post_status