Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
        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;
            }
            
        }
Posted
Updated 4-Sep-14 5:21am
v2
Comments
MuhammadUSman1 4-Sep-14 1:57am    
Check Solution.

-> M.U
CPallini 4-Sep-14 2:13am    
And what is exactly your problem in doing that? I mean if the used typed 'foo', cannot you simply match such a string with your string list?
Manoj Chamikara 4-Sep-14 2:31am    
Thank your reply
i want check key combination for a example user enter "asd" in order when trigger something and input"as" another and input like "ase" different something like that any idea how should i do that?

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
 
Share this answer
 
Comments
Manoj Chamikara 4-Sep-14 2:15am    
thanks your solution i tried key event it check one by one key but i want check key combination for a example user enter "asd" in order when trigger something and input"as" another and input like "ase" different something like that any idea how should i do that
Sergey Alexandrovich Kryukov 4-Sep-14 2:29am    
You need to use stateful approach: after each press, remember it and handle next press using the information on previously pressed keys...
—SA
Manoj Chamikara 4-Sep-14 4:22am    
can you more explain please
Manoj Chamikara wrote:
can you more explain please
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.

[EDIT]

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

—SA
 
Share this answer
 
v2
Comments
Manoj Chamikara 4-Sep-14 11:28am    
Thank You Very Much Your Grate Response to My Question I Update My Question with my code sample as you say using StringBuilder but first time it works perfectly for example fist time user input "A" then label1 shows it then user type "AS" then label show it but user again type "A" it doesn't show in label what is the wrong in my code?
Sergey Alexandrovich Kryukov 4-Sep-14 17:29pm    
You code has a number of problems; and it is too much of ad hoc in general. First of all, don't use KeyDown, use KeyPress. There is a big difference. KeyPress will give you only the characters, not keys. You need characters.
I strongly advise you do exactly what I told you, with clear logic.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900