Click here to Skip to main content
15,908,775 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Basically i have a code for detecting user inputs of alphabets and numbers
C#
private void KeyPressForm1(object sender, KeyPressEventArgs e)
{
    Settings.Keypressed = e.KeyChar;
    label5.Text = "" + Settings.Keypressed;
}

private void KeyUpForm1(object sender, KeyEventArgs e)
{
    label5.Text = "";
}

However suppose in this application i have a button, this code does not work because the button gets automatically selected, it gains the blue shade.
I want to make is so when the button is pressed it works than in unselected

What I have tried:

Basically i have tried removing the button all together but i like using the look of a button
Posted
Updated 21-Jun-19 4:57am
Comments
BillWoodruff 20-Jun-19 17:21pm    
The "button gets automatically selected" when what ?

You need to clearly define the conditions under which you want to detect the KeyPress: all the time, for any control ? only for one, or some, controls ?
NotAComputerScienceStudent 21-Jun-19 6:32am    
https://youtu.be/AtE7xRmQAjc
Visual representataion
BillWoodruff 21-Jun-19 7:01am    
I can't understand the video. Try to answer the questions I asked you.

Exactly when do you want to get KeyPress Events ?
NotAComputerScienceStudent 21-Jun-19 7:16am    
i want them to happen when ever i press a key. However when i have a button in the form, the key press event does not work
BillWoodruff 21-Jun-19 10:58am    
See if the solution I just posted works for you.

I solution i came up with was using a label instead of a button which also works, but plz do add other ideas.
 
Share this answer
 
v2
Comments
Maciej Los 20-Jun-19 6:50am    
If you have solved your issue, please mark your answer as a solution. This will change the status of your question to "solved".
 
Share this answer
 
Comments
BillWoodruff 20-Jun-19 17:22pm    
I would bet that disabling Control selection is not the real issue here.
Maciej Los 21-Jun-19 3:37am    
We'll see...
Set the 'FormKeyPreview Property to 'false. Try this in the Form:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    // only to demonstrate you are getting all the characters
    // no matter what Control has the focus
    Console.WriteLine($"active control: {ActiveControl.Name} | key: {keyData}");
            

    // handle the characters here

    return base.ProcessCmdKey(ref msg, keyData);
}
Quote:
The ProcessCmdKey method first determines whether the control has a ContextMenu, and if so, enables the ContextMenu to process the command key. If the command key is not a menu shortcut and the control has a parent, the key is passed to the parent's ProcessCmdKey method. The net effect is that command keys are "bubbled" up the control hierarchy. In addition to the key the user pressed, the key data also indicates which, if any, modifier keys were pressed at the same time as the key. Modifier keys include the SHIFT, CTRL, and ALT keys.
[^]
 
Share this answer
 
v2

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