在其他程序中人为生成关键事件

发布于 2024-11-10 13:41:54 字数 436 浏览 4 评论 0原文

具有焦点的 C# 程序是否可以人为地生成将由另一个打开的应用程序处理的按键事件?

对于那些好奇的人,这就是我问的原因:

我目前正在制作基于手势的媒体播放器的设计理念的演示/模型,使用 Kinect 作为主要输入设备。这是我决定做的事情,作为一个帮忙,就编程而言,它不需要也不值得我花费大量时间。

因此,为了加快速度,我使用了 FAAST 工具,您可以使用它轻松地将按键事件绑定到某些手势。因此,您打开该程序,告诉它哪个键映射到哪个手势,然后当您做出手势时,该键将被注册为由当前应用程序按下。

我想要创建的是一个中间人,它将显示一个简单的 GUI,我将向观众展示该 GUI。在某个时刻,用户将做出“选择”手势,我希望音乐开始在打开的媒体播放器中播放。因此,我会检查它们在菜单层次结构中的位置,看看它们是否根据我选择的任意键选择了“音乐”,然后使用 MediaPlayPause 键生成一个键事件。

Can a C# program that has focus artificially generate a key event that will be handled by another open application?

For those who are curious, this is why I'm asking:

I'm currently working on a demo/mockup for a design idea for a gesture based media player, using the Kinect as a primary input device. This is something I decided to do on as a favor, and it neither needs nor deserves a ton of my time as far as programming goes.

So to speed things along, I'm using the FAAST tool which you can use to easily bind key events to certain gestures. So, you turn on this program, tell it what key maps to what gesture, then when you make the gesture the key is registered as pressed by the current application.

What I would like to create is a go-between that will display a simple GUI that I'll display to the audience. At a certain point, the user will make the "select" gesture, and I want music to start playing in an open media player. So, I would check their place in the menu hierarchy, see that they've selected "music" based on some key I choose to arbitrarily mean select, then generate a key event with the MediaPlayPause key.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独木成林 2024-11-17 13:41:54

它需要从 C# 执行一些 p/invoke 工作,但并不复杂。您将使用 FindWindow API 获取目标程序的 hWnd,然后PostMessage API 发送按键事件:

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
private static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

private const UInt32 WM_KEYDOWN = 0x0100;
private const UInt32 WM_KEYUP = 0x0101;

public static void SendSelect(IntPtr HWnd)
{
    PostMessage(HWnd, WM_KEYDOWN, KeyInterop.VirtualKeyFromKey(System.Windows.Input.Key.Enter), 0);
}

It requires a little p/invoke work to do from C# but it's not complicated. You'll use the FindWindow API to get an hWnd for the target program, then the PostMessage API to send key events:

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
private static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

private const UInt32 WM_KEYDOWN = 0x0100;
private const UInt32 WM_KEYUP = 0x0101;

public static void SendSelect(IntPtr HWnd)
{
    PostMessage(HWnd, WM_KEYDOWN, KeyInterop.VirtualKeyFromKey(System.Windows.Input.Key.Enter), 0);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文