在我的WPF应用程序不具有焦点捕捉按键事件 [英] Capturing Key Press Event in my WPF Application which do not have focus

查看:222
本文介绍了在我的WPF应用程序不具有焦点捕捉按键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了在WPF的屏幕键盘。我需要以保持锁,移
等(它们是否按下)一个大写的轨道捕获键
按事件(通过键盘)。
请注意,我的应用程序失去任何其他应用程序
(比如记事本)打开时关注的焦点。

I have developed an On Screen Keyboard in WPF. I need to capture the key press event (via Key Board) in order to keep a track of Caps Lock, Shift etc (whether they are pressed). Please note that my application loses focus when any other application (say notepad) is opened.

任何人都可以建议如何在WPF中实现这一点?总之
,我的WPF应用程序需要捕获按键事件甚至
虽然它不具有焦点。 。请帮助

Could anyone suggest how to achieve this in WPF? in short, my WPF application needs to capture the key press events even though it does not have focus. Kindly help.

推荐答案

我用背后一个简单的代码:

I use a simple code behind:

在XAML中我使用了一个名为MyTestKey KeyDown事件

In the xaml I use a KeyDown event called MyTestKey.

<Window x:Class="MyApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        KeyDown="myTestKey" >

这是该keydown日常的样子,我检查了1号:

This is what the keydown routine looks like where I check for the number 1:

private void myTestKey(object sender, KeyEventArgs e)
{
            if ((e.Key == Key.D1) || (e.Key == Key.NumPad1))
            {
                //do some stuff here
                return;
            }
}

这是一种简单的方式来获得任意键。我希望这有助于。

This is an easy way to get to any key. I hope this helps.

这篇关于在我的WPF应用程序不具有焦点捕捉按键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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