Jump to content
  • Advertisement
Sign in to follow this  
shawnre

[.net] Capturing Return/Enter and KeyCode for "="

This topic is 3568 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all, So basically, I am just creating a standard calculator with 0-9, decimal point, addition and subtraction (on a WinForm, using C#). I want the user to be able to use the keyboard, or use the buttons to enter their values. Here is the strange problem. I had to turn KeyPreview(on the Form) to true to start capturing the NumberPad/Keyboard, however doing that for the form seems to disable the capturing of the enter/return key. Anyone know how to do that? I have tried so many ways now, here is the code I have: LINK One other problem is the KeyCode for the "=" key. Is there not one? Does it default to enter/return? Thanks, Shawn

Share this post


Link to post
Share on other sites
Advertisement
This sounds a lot like homework, so I'll just point you in the right direction.

To get to key events that are usually interpreted/handled before you get the chance, you might need to hook into the command processor at an earlier stage.

You'll need to inherit the form and create an override.
Here is a quick example.


protected override bool ProcessCmdKey(ref Message msg, Keys KeyCode)
{
switch ((int)KeyCode)
{
case (int)Keys.Up:
if (this.onUpArrow != null)
this.onUpArrow();
break;
default:
return base.ProcessCmdKey(ref msg, KeyCode);
}
}



The key code can be learned by just writing an application that does a messagebox.show() with the keycode of a given keydown event in it.

Note: that if this is homework, then the above solution is way overkill for your problem. You might consider just going with a simpler solution by just using a different button.

Share this post


Link to post
Share on other sites
Nope, not at all homework. My brother uses the Calculator supplied with XP to balance his check book. His vision isnt that great, so the buttons are hard for him to see. So for some programming practice I decided to make him a calculator with big buttons and fonts. I did get it working for the most part though, but I will look into ProcessCmdKey method that you alluded to just to further my knowledge.

Thanks,

Shawn

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!