locked
Need help assigning a key to a button RRS feed

  • Question

  • I want to have a windows form, where no matter what is in focus or what is going on when I press "F5" it is like a click from a button on the form. Or some event that I can call the same method the button click event handler calls. Either way I can't figure it out at all

    Monday, March 16, 2009 10:42 PM

Answers

  •  
            protected override bool ProcessCmdKey(ref Message msg, Keys keyData)  
            {  
                if ((keyData & Keys.KeyCode) == Keys.F5)  
                {  
                    button1.PerformClick();  
                    return true// Key has been processed.  
                }  
                else 
                    return base.ProcessCmdKey(ref msg, keyData);  
            } 
    • Proposed as answer by JohnGrove Tuesday, March 17, 2009 12:14 AM
    • Marked as answer by mscheaf Tuesday, March 17, 2009 2:02 PM
    Monday, March 16, 2009 11:00 PM

All replies

  •  
            protected override bool ProcessCmdKey(ref Message msg, Keys keyData)  
            {  
                if ((keyData & Keys.KeyCode) == Keys.F5)  
                {  
                    button1.PerformClick();  
                    return true// Key has been processed.  
                }  
                else 
                    return base.ProcessCmdKey(ref msg, keyData);  
            } 
    • Proposed as answer by JohnGrove Tuesday, March 17, 2009 12:14 AM
    • Marked as answer by mscheaf Tuesday, March 17, 2009 2:02 PM
    Monday, March 16, 2009 11:00 PM
  • Hello mscheaf,
        Alternatively use this link which has some helpful tutorials on capturing key presses, regardless of focus.

    Hope this helps!
    Thanks!
    chukrum47

    P.S. Two responses to two of your threads in two minutes!!
    How are a plum and a rabbit similar? They're both purple, except for the rabbit.
    Tuesday, March 17, 2009 2:05 AM
  • BinaryCoder said:

     

            protected override bool ProcessCmdKey(ref Message msg, Keys keyData)  
            {  
                if ((keyData & Keys.KeyCode) == Keys.F5)  
                {  
                    button1.PerformClick();  
                    return true// Key has been processed.  
                }  
                else 
                    return base.ProcessCmdKey(ref msg, keyData);  
            } 



    this worked. I don't know why though haha! I have never heard of this method nor do I know why it gets run when I press the key. I am going to do a little reading about it. Thats the nice thing about .net. Usually there is always something I haven't seen yet that does what I want, it is just a matter of finding it. Thanks
    Tuesday, March 17, 2009 2:02 PM
  • BinaryCoder said:

     

            protected override bool ProcessCmdKey(ref Message msg, Keys keyData)  
            {  
                if ((keyData & Keys.KeyCode) == Keys.F5)  
                {  
                    button1.PerformClick();  
                    return true// Key has been processed.  
                }  
                else 
                    return base.ProcessCmdKey(ref msg, keyData);  
            } 



    Actually now I am having problems with this. I set something to Keys.Enter which worked until I scanned a credit card. The CC scanner works like a keyboard and at the end of the cc info it has a "?".  this keyData for this shows up as LButton | MButton | Back. The Keys.Enter has the same value. This has the effect of running my method I have tied to the Enter key.

    Why is the ? showing up as Lbutton, Mbutton, back?? When I capture the question mark in a KeyPressEventArgs object it just shows up as 63 '?'.

    Why does Keys.Enter equate to LButton | MButton | Back? Why can't it just be enter?

    I used to like winforms, but every day I find something else about it that just doesn't do what I would expect or want. If I say I want to capture when someone hits enter, why in the world would I want to capture all those other keystrokes????
    • Proposed as answer by c89mathe Thursday, April 23, 2009 8:31 AM
    Thursday, March 19, 2009 5:48 PM
  • Try to use msg instead of keydata!
    Good luck!

    /Mats

            protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
            {
                int key = message.WParam.ToInt32();
                if (key == (int)Keys.Enter)
                {
                    button1.PerformClick();
                    return true; // Key has been processed. 
                }
                else
                    return base.ProcessCmdKey(ref msg, keyData);
            }
    Thursday, April 23, 2009 8:33 AM