python keyboard モジュールでホットキーを設定 する方法をChatGPT(無料)さんに聞いてみた

python keyboard モジュールの使い方を効いてみた
https://github.com/boppreh/keyboard


あとで試す

複数のキーの検出: Python


ユーザー
Windows10でのPythonプログラミングについての質問です。マルチキーボード使用時にどのキーが押されたかを検出するにはどうすればよいですか?

ChatGPT
Python を使用して Windows 10 で複数のキーボードを使用するときにどのキーが押されたかを検出するには、モジュールを使用できますkeyboard。このモジュールは、キーボード イベントをキャプチャして処理する機能を提供します。

以下は、複数のキーボードからのキー押下を検出する方法を示すコード例です。

パイソン

コードをコピーする
import keyboard

# Define the event handler function
def on_key_event(event):
print(f"Key {event.name} was {'pressed' if event.event_type == 'down' else 'released'} on {event.device.name}")

# Add event handler for all keyboards
for device in keyboard.devices():
keyboard.hook(device.id, on_key_event)

# Start listening for events
keyboard.wait()
このコードでは、最初にkeyboardモジュールをインポートします。次に、キーが押されるか離されるたびに呼び出されるイベント ハンドラー関数を定義します。イベント ハンドラー関数は、キーの名前と、それが押されたデバイスの名前を単純に出力します。

次に、関数を使用してすべてのキーボードを反復処理しkeyboard.devices()、関数を使用してキーボードごとにイベント ハンドラーを追加しますkeyboard.hook()。最後に、関数を使用してイベントのリッスンを開始しますkeyboard.wait()。この関数は、キーボードのキーが押されるか放されるまでプログラムをブロックします。

このコードを実行すると、接続されているキーボードのいずれかのキーを押したり放したりするたびに、キーの名前と、そのキーが押されたまたは放されたデバイスの名前が出力されます。





ユーザー
Windows で生成されたイベントはデバイス ID を報告しません (event.device == None)。https://github.com/boppreh/keyboard/issues/21

ChatGPT
あなたは正しいです。このkeyboardモジュールは、複数のキーボードから生成されたイベントの Windows 上のデバイス ID 情報を提供しません。これはモジュールの既知の問題であり、keyboard前述したように GitHub ページに文書化されています。

回避策として、パッケージのwin32apiおよびwin32conモジュールを使用してpywin32、各キーボードのデバイス ID を手動で取得できます。各キーボードのデバイス ID を取得し、それを使用してどのキーボードがキー押下イベントを生成したかを識別する方法を示すコード例を次に示します。

パイソン

コードをコピーする
import keyboard
import win32api
import win32con

# Get the device ID for each keyboard
keyboard_devices = [device for device in keyboard.devices()]
device_id_map = {}
for device in keyboard_devices:
device_handle = win32api.OpenDesktop(device, 0, False, win32con.MAXIMUM_ALLOWED)
device_id = win32api.GetWindowThreadProcessId(device_handle)[-1]
device_id_map[device_id] = device

# Define the event handler function
def on_key_event(event):
device_name = device_id_map.get(event.scan_code, "Unknown device")
print(f"Key {event.name} was {'pressed' if event.event_type == 'down' else 'released'} on {device_name}")

# Add event handler for all keyboards
for device in keyboard_devices:
keyboard.hook(device.id, on_key_event)

# Start listening for events
keyboard.wait()
このコードでは、最初にkeyboard.devices()関数を使用して、接続されているすべてのキーボードのリストを取得します。次に、リストを反復処理し、モジュールOpenDesktop()の およびGetWindowThreadProcessId()関数を使用してwin32api、各キーボードのデバイス ID を取得します。デバイス ID と名前を という辞書に保存しますdevice_id_map。

device_id_map次に、 を使用して、デバイス ID に基づいてイベントを生成したデバイスの名前を検索するイベント ハンドラー関数を定義します。次に、イベント ハンドラー関数は、キーの名前と、キーが押されたまたは放されたデバイスの名前を出力します。

関数を使用してすべてのキーボードにイベント ハンドラーを追加しkeyboard.hook()、関数を使用してイベントのリッスンを開始しますkeyboard.wait()。

このコードを実行すると、接続されているキーボードごとに、キーの名前と、キーが押されるか離されたデバイスの名前が出力されます。





メッセージを送ります。


無料研究のプレビュー。ChatGPT は、人、場所、または事実に関する不正確な情報を生成する可能性があります。ChatGPT 5月3日バージョン



原文
Detecting Multiple Keys: Python


User
question about Python programming in Windows10 .

how to detect witch key when using multi keyboard?

ChatGPT
To detect which key is pressed when using multiple keyboards in Windows 10 using Python, you can use the keyboard module. This module provides functions to capture and handle keyboard events.

Here's an example code that shows how to detect key presses from multiple keyboards:

python
Copy code
import keyboard

# Define the event handler function
def on_key_event(event):
print(f"Key {event.name} was {'pressed' if event.event_type == 'down' else 'released'} on {event.device.name}")

# Add event handler for all keyboards
for device in keyboard.devices():
keyboard.hook(device.id, on_key_event)

# Start listening for events
keyboard.wait()
In this code, we first import the keyboard module. We then define an event handler function that will be called whenever a key is pressed or released. The event handler function simply prints out the name of the key and the name of the device on which it was pressed.

We then iterate over all the keyboards using the keyboard.devices() function and add an event handler for each one using the keyboard.hook() function. Finally, we start listening for events using the keyboard.wait() function, which blocks the program until a key is pressed or released on any of the keyboards.

When you run this code, it will print out the name of the key and the name of the device on which it was pressed or released, whenever you press or release a key on any of the connected keyboards.





User
Events generated under Windows don't report device id (event.device == None).

https://github.com/boppreh/keyboard/issues/21

ChatGPT
You are correct. The keyboard module does not provide device ID information on Windows for events generated from multiple keyboards. This is a known issue in the module, and it is documented on the keyboard GitHub page, as you mentioned.

As a workaround, you can use the win32api and win32con modules from the pywin32 package to get the device ID for each keyboard manually. Here's an example code that shows how to get the device ID for each keyboard and use it to identify which keyboard generated a key press event:

python
Copy code
import keyboard
import win32api
import win32con

# Get the device ID for each keyboard
keyboard_devices = [device for device in keyboard.devices()]
device_id_map = {}
for device in keyboard_devices:
device_handle = win32api.OpenDesktop(device, 0, False, win32con.MAXIMUM_ALLOWED)
device_id = win32api.GetWindowThreadProcessId(device_handle)[-1]
device_id_map[device_id] = device

# Define the event handler function
def on_key_event(event):
device_name = device_id_map.get(event.scan_code, "Unknown device")
print(f"Key {event.name} was {'pressed' if event.event_type == 'down' else 'released'} on {device_name}")

# Add event handler for all keyboards
for device in keyboard_devices:
keyboard.hook(device.id, on_key_event)

# Start listening for events
keyboard.wait()
In this code, we first use the keyboard.devices() function to get a list of all connected keyboards. We then iterate over the list and use the OpenDesktop() and GetWindowThreadProcessId() functions from the win32api module to get the device ID for each keyboard. We store the device ID and name in a dictionary called device_id_map.

We then define an event handler function that uses the device_id_map to look up the name of the device that generated the event, based on the device ID. The event handler function then prints out the name of the key and the name of the device on which it was pressed or released.

We add the event handler for all keyboards using the keyboard.hook() function, and start listening for events using the keyboard.wait() function.

When you run this code, it should print out the name of the key and the name of the device on which it was pressed or released, for each connected keyboard.




Send a message.


Free Research Preview. ChatGPT may produce inaccurate information about people, places, or facts. ChatGPT May 3 Version