如何在C#中找到按键组合 [英] How Do I Find Key Combination Pressed In C#

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

问题描述

我正在使用C#windows窗体应用程序

这是我想要找到的问题组合键

示例用户按下了一些单词然后我希望与我的组合实时匹配

基本上我想在实时用户输入数据中匹配这些单词

我怎么能这样做我知道我实时卡在匹配组合中



例子 - aaa asaeraa



提前感谢!



问题更新 - 感谢Sergey Alexandrovich Kryukov



I'm Using C# windows form application
This is the my problem I want find key combination
for a example User pressed some words then i want match with my combinations in real time
basicly i want to Match these words in real time user input data
how can i do it any idea I'm stuck in match combinations in real time

example - "aaa" "as" "aer" "aa"

Thank in advance!

Question Update-- thanks to Sergey Alexandrovich Kryukov

        private StringBuilder wordcharset = new StringBuilder();
        
        void KeyDown(object sender, KeyEventArgs e)
        {
            if (wordcharset.Length < 4)
            {
                wordcharset.Append(e.KeyCode.ToString());
                match(wordcharset.ToString());
            }
            else
            {
                wordcharset.Clear();
                wordcharset.Append(e.KeyCode.ToString());
                match(wordcharset.ToString());
            }

            e.Handled = true;
        }


private void match(string wor)
        {
            switch (wor)
            {
                case "A":
                        label1.Text = "A";
                    break;

                case "AS":
                        label1.Text = AS"";
                    break;

                case "AER":
                        label1.Text = "AER";                     
                    break;
            }
            
        }

推荐答案

请检查c#中的关键事件。



这里所有活动也会检查每个活动并使用你想要的。



使用键盘事件



如果有任何问题然后让我知道。



- > MU
Please check Key events in c#.

Here is all events also check each and use which you want.

Using Keyboard Events

if any issue then let me know.

-> M.U


Manoj Chamikara写道:
Manoj Chamikara wrote:

你能解释一下吗

键盘事件处理程序可以一次处理一个按键(只有Alt,Ctrl或Shift等状态键会自动处理,并且它们的状态在其事件参数中可供处理程序使用)并且是无状态的。



您必须向其添加状态。比如说,您可以添加表单或其他控件类的实例成员,以获得先前按下的键的容器。我建议只使用 System.Text.StringBuilder 。在您的情况下,您可能只需要处理事件 KeyPress 。处理程序应该接受一个字符并将其添加到容器中,每次尝试识别整个序列(aer,aa等)。一旦识别出某些内容,请清理字符容器并执行相应的操作。对于actions容器,可以使用 System.Collection.Generic.Dictinary< string,System.Action> ,其中字符串键是命令,如aa, value参数是用于处理每个命令的委托实例。它可能更复杂,缩写为命令等。



重要提示:您需要通过其他一些标准清除字符容器,或制作循环缓冲区否则,未识别的字符最终会占用太多内存。我建议你总是在容器的字符数等于最长的命令时清理容器:如果命令被识别,则不再需要它,如果没有,所有字符都可以被认为是无用的,你重新开始。你可以创建一些更智能的算法。



如果不清楚,请不要犹豫,询问您的后续问题。你的问题很合理,所以我很乐意帮助你。







数据和操作之间的关联,请参阅我的文章:动态方法调度程序 [ ^ ]。



-SA

The keyboard event handler can deal with one press at a time (only state keys like Alt, Ctrl or Shift are automatically handled and their states are available to your handler in its event argument) and is stateless.

You have to add the state to it. Say, you can add an instance member of your form or another control class to have a container of previously pressed keys. I would suggest simply using System.Text.StringBuilder. In your cases, you probably only need to handle the event KeyPress. The handler should take a character and add it to the container, each time trying to recognize the whole sequence ("aer", "aa", etc). As soon as you recognize something, clean up the character container and do corresponding action. For actions container, you can use System.Collection.Generic.Dictinary<string, System.Action>, where the string key is the "command", like "aa", and the value parameter is the delegate instance used to handle each "command". It can be more complex, abbreviated "commands", etc.

Important: you will need to clear up the character container by some other criteria, or make a cyclic buffer, otherwise non-recognized characters can occupy too much memory eventually. I suggest that you always clean the container when it has the number of characters equal to your longest command: if the command is recognized, its no longer needed, if not, all characters can be considered useless and you start over. You get create some smarter algorithm.

Please don't hesitate to ask your follow-up question if something is not clear. Your question is quite reasonable, so I'll gladly help you.



For associations between data and actions, please see my article: Dynamic Method Dispatcher[^].

—SA


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

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