Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating C# Windows Application, in that for a button i need to assign shortcut keys.
My client requirement is shorcut keys should work for both ex: Alt + 1 and Alt + NUMPAD 1 .

How to achieve this.
Posted

try this

C#
private void Form1_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            //Suppose when User Press Ctrl + J then Click Button1
            if (e.Control == true && e.KeyCode == Keys.J)
            {
                button1.PerformClick();
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Me Pressed By Shortcut !!");
        }



All the best
Gopinath
 
Share this answer
 
Comments
Member 9929607 21-Mar-13 5:51am    
I will Clear my question one more time.
I am using Menu strip and for each menu item i am supposed to add shortcut keys.
My requirement is to one of the menu item , shortcut key is ALT + 1. If the end user presses
ALT + 1 then it will work. Instead of ALT + 1 if end user presses ALT + NUMPAD 1 then its not working.

Thank You.
in button property

Text=&Submit//here s is the shortcut key.if you want to change ambson sybol change to anoterh.

other wise above solution(i.e solution1) is correct
 
Share this answer
 
v2
Comments
Member 9929607 21-Mar-13 5:51am    
I will Clear my question one more time.
I am using Menu strip and for each menu item i am supposed to add shortcut keys.
My requirement is to one of the menu item , shortcut key is ALT + 1. If the end user presses
ALT + 1 then it will work. Instead of ALT + 1 if end user presses ALT + NUMPAD 1 then its not working.

Thank You.
First you should change the Key Preview properties of the form to true.
Then at the Key_Down event of the form you should try this.
C#
if (e.Control && e.KeyCode == Keys.M)      
            {
               Perform the operation that you want to do.
Form 2 =new Form();   //here Ctrl+M will open form .Change it according to your need.
2.Show();
            }
if (e.Alt && e.KeyCode == Keys.M)      
            {
               Perform the operation that you want to do.
Form 2 =new Form();   //here Alt+M will open form .Change it according to your need.
2.Show();
            }


Thanks
 
Share this answer
 
v2
Comments
Member 9929607 21-Mar-13 5:51am    
I will Clear my question one more time.
I am using Menu strip and for each menu item i am supposed to add shortcut keys.
My requirement is to one of the menu item , shortcut key is ALT + 1. If the end user presses
ALT + 1 then it will work. Instead of ALT + 1 if end user presses ALT + NUMPAD 1 then its not working.

Thank You.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900