键盘挂钩获取键组合(WPF) [英] Keyboard hook get key combination (WPF)

查看:178
本文介绍了键盘挂钩获取键组合(WPF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在此处使用此帖子:在WPF/C#中使用全局键盘挂钩(WH_KEYBOARD_LL) 而且我成功地完成了此任务.

I tried using this post here: Using global keyboard hook (WH_KEYBOARD_LL) in WPF / C# And i have this sucessfully working.

但是有些事情我无法摆脱. 它可以检测到每一个按键,但是我想让我的应用程序在按键组合上执行某些操作.

But there is something i cant get my finger behind. It can detect every single key pressed, but i like to make my app do something on a key combination.

void KListener_KeyDown(object sender, RawKeyEventArgs args)
        {
            Console.WriteLine(args.Key.ToString());
            if (args.Key == Key.LeftCtrl && args.Key == Key.C)
            {
                MessageBox.Show(args.Key.ToString());
            }
        }

很明显,这是行不通的,因为空值仅适用于每个键(如果我理解正确的话)

Obvious, this does not work, as the void only is for every single key (if i understand correct)

因此,我真的需要一些帮助来使其适用于组合键,例如Ctrl + C 有人可以在这里将我推向正确的方向吗?

So i really need some help to get it working for a key combination, for example Ctrl + C could someone push me in the right direction here ?

推荐答案

存储所按下键的值,并在下次调用您的方法时,检查此存储值和实际值是否为您的键组合.

Store the value of the key that is pressed and the next time your method is called check if this stored value and the actual value are your key combination.

    var lastKey;
void KListener_KeyDown(object sender, RawKeyEventArgs args)
        {

            Console.WriteLine(args.Key.ToString());
            if (lastKey == Key.LeftCtrl && args.Key == Key.C)
            {
                MessageBox.Show(args.Key.ToString());
            }
           lastKey = args.Key;
        }

这篇关于键盘挂钩获取键组合(WPF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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