locked
Having a Keyboard event graphically 'Click' a WinForms Button RRS feed

  • Question

  • I'm trying to generate a calculator, and as one of the features, when a person selects a 3 for example on their keyboard, I want the corresponding on screenbutton to physically depress and release on screen, so they can see the numbers being entered in as they use the keyboard.

    I've looked at the PerformClick() method, and while that does call the appropriate click event, I have not been able to have it graphically depress and release the button on screen.

    How can I graphically depress the WinForms buttons on a keyboard event.

    As an added bonus, it would be nice to be able to have the button depressed on keydown, and stay depressed until keyup.

    Wednesday, January 26, 2011 9:50 PM

Answers

  • Steve,

    Thanks for the reply. I figured that's what I would have to do-> set the button to an image and have a swapping of images during key down and up events.

    • Marked as answer by Helen Zhou Friday, February 4, 2011 9:24 AM
    Thursday, January 27, 2011 5:39 PM

All replies

  • Hi David,

    Please refer to this article on MSDN: How to: Simulate Mouse and Keyboard Events in Code. As for such fancy effect, I'd recommend you to use WPF other than WinForm. 

    For third party calculator, you can refer to such as Extended Calculator 2.03 , it is an advanced desktop calculator with graph drawing and programming capabilities.


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Thursday, January 27, 2011 3:35 AM
  • Hi David,

    A couple of ideas, though I haven't tried them myself:

    1. Set the property KeyPreview=True for the form containing the calculator buttons.

    2. In the keydown event for the form trap the keypress and determine which numeric key was pressed. Use this information to set the image of the appropriate calculator button to a depressed image. You could also set focus to the button using the Focus method and call sendkeys to simulate pressing the enter button.

    3. In the keyup event for the form, trap they keypress again and determine which numeric key was pressed and use this information to set the appropriate calculator button to an un-depressed image. You could build in a short delay so that the visual impact of the "button press" is longer. 

    Using an imagelist and paintbrush to create depressed/un-depressed images of buttons should make the task easier.

    Hope this helps,

    Steve G

     


    Steve G
    Thursday, January 27, 2011 8:40 AM
  • Steve,

    Thanks for the reply. I figured that's what I would have to do-> set the button to an image and have a swapping of images during key down and up events.

    • Marked as answer by Helen Zhou Friday, February 4, 2011 9:24 AM
    Thursday, January 27, 2011 5:39 PM
  • Hi David,

    SendKeys() will  emulate pushing a key. But Personally I find it a huge waste of time to complicated things like this.


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, January 28, 2011 8:59 AM