Reputation: 79
I'm trying to have 2 different message box display when "Ctrl+l" is pressed and another when "Shift+A" is pressed as well. I have everything done but when i press these button while the program is running, nothing happens. I'm not sure what i have done wrong.
My code as follows:
public Color()
{
InitializeComponent();
ContextMenuStrip s = new ContextMenuStrip();
ToolStripMenuItem directions = new ToolStripMenuItem();
directions.Text = "Directions";
directions.Click += directions_Click;
s.Items.Add(directions);
this.ContextMenuStrip = s;
this.KeyDown += new KeyEventHandler(Color_KeyDown);//Added
}
void directions_Click(object sender, EventArgs e)
{
MessageBox.Show("Just click on a color chip to see the translation");
}
//Keypress
private void Color_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.L && (e.Control))
{
MessageBox.Show("You can choose from four different languages by clicking on the radio buttons");
}
else if (e.KeyCode == Keys.A && (e.Shift))
{
MessageBox.Show("This is version 1.0");
}
}
Upvotes: 1
Views: 8543
Reputation: 8782
If you want to capture command keys in your form or control you have to override the ProcessCmdKey
method. In your form use this code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Shift | Keys.A))
{
}
else if (keyData == (Keys.Control | Keys.I))
{
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
Here you can find more about processing command keys.
Upvotes: 2
Reputation: 36
Did you subscribe your handler (Color_KeyDown) to an event such as PreviewKeyDown? I see you hooked up directions_Click to listen for mouse clicks on your ToolStripMenuItem called "directions" control, but from your code, I don't see where you're listening for key events.
Try adding a line similar to:
myWinFormsControl.PreviewKeyDown += Color_KeyDown;
where myWinFormsControl is the window or control you want to invoke your handler whenever a keypress event fires. Note: make sure you give the control input focus when testing (for example if it's a textbox, it won't fire a keypress event unless the text cursor is inside prior to pressing the key).
Another helpful trick if you're using visual studio is you can select a control while in design view and open the properties pane and click the little lightning bolt icon to see all the see all the available events for that control. You can also double click inside one of the empty sells to add a new event handler. (see here for more details)
Quick example of getting keypress events from a textbox in winforms:
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
TextBox someTextBox = new TextBox();
someTextBox.PreviewKeyDown += someTextBox_PreviewKeyDown;
Form myMainWindow = new Form();
myMainWindow.Controls.Add(someTextBox);
Application.Run(myMainWindow);
}
static void someTextBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.L && (e.Control))
{
MessageBox.Show("You can choose from four different languages by clicking on the radio buttons");
}
else if (e.KeyCode == Keys.A && (e.Shift))
{
MessageBox.Show("This is version 1.0");
}
}
}
and to just catch at the form level (aka as long as the window has input focus):
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form myMainWindow = new Form();
myMainWindow.PreviewKeyDown += myForm_PreviewKeyDown;
Application.Run(myMainWindow);
}
static void myForm_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.L && (e.Control))
{
MessageBox.Show("You can choose from four different languages by clicking on the radio buttons");
}
else if (e.KeyCode == Keys.A && (e.Shift))
{
MessageBox.Show("This is version 1.0");
}
}
}
Upvotes: 0