媒体播放/暂停模拟 [英] Media Play/Pause Simulation

查看:30
本文介绍了媒体播放/暂停模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的键盘包含一行用于各种非标准键盘任务的按钮.这些键包含修改音量、播放或暂停以及跳过曲目等功能.如何使用 Python 模拟基本播放/暂停?顺便说一下,我使用的是 Windows.

解决方案

我会使用 pywin32.与安装捆绑在一起的是大量的 API 文档(通常放置在诸如 C:\Python32\Lib\site-packages 之类的地方.)它本质上在 Win32 库中包装了很多东西,用于 Windows 中的许多低级任务.

安装后,您可以使用 keybd_event 的包装器.

您也可以使用 SendInput 代替 keybd_event 但它似乎没有被 PyWin32 包裹.SendMessage 也是一个选项,但比较麻烦.

您需要查找那些特殊按钮的虚拟扫描代码,因为我怀疑字符到代码的映射功能是否会在此处对您有所帮助.您可以在此处找到参考.

那么就是简单的调用函数了.下面的代码片段在我的电脑上暂停了 Chuck Berry.

<预><代码>>>>导入 win32api>>>VK_MEDIA_PLAY_PAUSE = 0xB3>>>hwcode = win32api.MapVirtualKey(VK_MEDIA_PLAY_PAUSE, 0)>>>硬件代码34>>>win32api.keybd_event(VK_MEDIA_PLAY_PAUSE, hwcode)

MapVirtualKey 为我们提供了 keybd_event 需要的硬件扫描代码(或者更有可能是键盘驱动程序.)

请注意,所有这些都被键盘驱动程序占用,因此您实际上无法控制击键发送的位置.使用 SendMessage,您可以将它们发送到特定窗口.媒体键通常无关紧要,因为它们会被音乐播放器等截获.

My keyboard contains a row of buttons for various non-standard keyboard tasks. These keys contain such functions as modifying the volume, playing or pausing, and skipping tracks. How can I simulate a basic play/pause with Python? I am on Windows, by the way.

解决方案

I would use pywin32. Bundled with the installation is a large number of API-docs (usually placed at something like C:\Python32\Lib\site-packages.) It essentially wraps a lot of stuff in the Win32-library which is used for many low-levels tasks in Windows.

After installing it you could use the wrapper for keybd_event.

You could also use SendInput instead of keybd_event but it doesn't seem to be wrapped by PyWin32. SendMessage is also an option but more cumbersome.

You'll need to look up the virtual scan code for those special buttons, since I doubt the char-to-code mapping functions will help you here. You can find the reference here.

Then it is a simple matter of calling the function. The snippet below pauses Chuck Berry on my computer.

>>> import win32api
>>> VK_MEDIA_PLAY_PAUSE = 0xB3
>>> hwcode = win32api.MapVirtualKey(VK_MEDIA_PLAY_PAUSE, 0)
>>> hwcode
34
>>> win32api.keybd_event(VK_MEDIA_PLAY_PAUSE, hwcode)

MapVirtualKey gives us the hardware scan code which keybd_event needs (or more likely, the keyboard driver.)

Note that all this is snapped up by the keyboard driver, so you don't really have any control where the keystrokes are sent. With SendMessage you can send them to a specific window. It usually doesn't matter with media keys since those are intercepted by music players and such.

这篇关于媒体播放/暂停模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆