gpt4 book ai didi

python - 如何在Windows中使用pynput按下媒体键?

转载 作者:行者123 更新时间:2023-12-01 07:40:54 25 4
gpt4 key购买 nike

我需要按播放媒体键,但找不到合适的键值

我尝试过这样做

from pynput.keyboard import Key, Controller
keyboard= Controller()
keyboard.press(Key.MediaPlayPause)
keyboard.release(Key.MediaPlayPause)

但这行不通我没有收到任何错误

错误回溯

Traceback (most recent call last):
File "2.py", line 63, in <module>
keyboard.press(Key.MediaPlayPause)
File "C:\Users\nebbu\AppData\Local\Programs\Python\Python37\lib\enum.py", line 349, in __getattr__
raise AttributeError(name) from None
AttributeError: MediaPlayPause

最佳答案

更新:

Since pynput version 1.5.0 media keys are available for use. The available keys are described here: (https://pynput.readthedocs.io/en/latest/keyboard.html#pynput.keyboard.Key.media_next)

<小时/>

媒体控制在 pynput 上仍然不可用。 (https://github.com/moses-palmer/pynput/pull/171)

您可以使用虚拟键代码发出键输入事件:

from pynput.keyboard import Controller, KeyCode


def main():
keyboard = Controller()

print('Pausing/Resuming... ')

keyboard.press(KeyCode.from_vk(0xB3)) # Play/Pause

if __name__ == "__main__":
main()

In the sample we are using Play/Pause virtual key code (0xB3). You can check the full list of possible values here https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes

关于python - 如何在Windows中使用pynput按下媒体键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56723541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com