Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
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 20-Mar-13 22:33pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

try this
 
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
  Permalink  
Comments
Member 9929607 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v2
Comments
Member 9929607 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
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
  Permalink  
v2
Comments
Member 9929607 at 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)

  Print Answers RSS
0 Mathew Soji 334
1 Maciej Los 275
2 BillWoodruff 260
3 Afzaal Ahmad Zeeshan 232
4 OriginalGriff 201
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,045


Advertise | Privacy | Mobile
Web04 | 2.8.1411019.1 | Last Updated 21 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100