view raw
Vinícius Gabriel Vinícius Gabriel - 6 months ago 35
C# Question

how to assign a numeric shortcut to a button o c#

I'm trying to assign a number key for a calculator project, how ever, it only accepts letters as shortcut.
Using a letter:

private void Calculator_KeyDown(object sender, KeyEventArgs e){
if(e.KeyCode.ToString() == "A"){

And it works fine, but what I really want to do is to replace "A" for "1", and when I do it doesn't work.
I also already tried this:

if(e.KeyCode == Keys.NumPad1){


Try this:

private void Form1_KeyDown(object sender, KeyEventArgs e)
    if (e.KeyCode == Keys.D1 || e.KeyCode == Keys.NumPad1)

Also if this KeyDown event is a handler for your Form I assume you've set your form's KeyPreview property to true, so your can intercept the event first on a form level.