如何在c#中获取组合键 [英] How to get a combination of keys in c#

查看:30
本文介绍了如何在c#中获取组合键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 C# 表单上捕获 Ctrl + Alt + K + P 键?谢谢

How can I capture Ctrl + Alt + K + P keys on a C# form? thanks

推荐答案

这是一个和弦,如果不记住和弦的第一个按键,你就无法检测到它.这有效:

It is a chord, you cannot detect it without memorizing having seen the first keystroke of the chord. This works:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }
    private bool prefixSeen;

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
        if (prefixSeen) {
            if (keyData == (Keys.Alt | Keys.Control | Keys.P)) {
                MessageBox.Show("Got it!");
            }
            prefixSeen = false;
            return true;
        }
        if (keyData == (Keys.Alt | Keys.Control | Keys.K)) {
            prefixSeen = true;
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
}

这篇关于如何在c#中获取组合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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