Sign in to follow this  
shawnre

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

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this