How to detect when arrow key down is pressed / C# WPF - c#

I'm working with WPF C# app, and I need to implement some action when arrow key down on keyboard is pressed, for example:
private void Window_KeyDown(object sender, KeyEventArgs e)
{
// Here I gotta check is that arrow key down which invoked this event.. then call a method
DoSomething();
}
I simply can not figure out in wpf how to detect a arrow key down ..
Any kind of help would be great!
Thanks !

The KeyEventArgs hold information about the pressed key in the KeyEventArgs.Key property and so you can check for the down arrow key by checking if e.Key is equal to Key.Down, which is the enumeration value for the arrow down key.
private void Window_OnKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Down) // The Arrow-Down key
{
DoSomething();
}
}

switch (e.Key)
{
case Key.Up:
break;
case Key.Down:
break;
case Key.Left:
break;
case Key.Right:
break;
default:
break;
}

Related

Handling multiple keys inside a switch block

I am developing a Windows Forms Application where I am trying to Hide a panel on whenever a user presses the combination of F12 and ctrl key but I am getting the error Operator '&&' cannot be applied to operands of type 'Keys' and 'Keys' . Thanks for your time.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
//method to assign keys
switch (e.KeyCode)
{
case Keys.Down:
SendKeys.Send("{Tab}");
e.Handled = true;
break;
case (Keys.Control && Keys.F12): **// error here**
this.panel3.Hide();
default:
break;
}
}
If you want to Hide() on Ctrl + F12 combination, you should check e.Modifiers:
...
case (Keys.F12): // On F12
if (e.Modifiers == Keys.Control) { // On Ctrl + F12
this.panel3.Hide();
}
...

C# Detect when key gets pressed and trigger a button

So I'm trying to make a simple calculator. The user can only input the numbers by the buttons on the form or by the numpad. This is the code I have:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
string key = "";
switch (e.KeyCode)
{
case (Keys.NumPad1):
key = "1";
break;
case (Keys.NumPad2):
key = "2";
break;
default:
break;
}
txt_string.Text = txt_string.Text + key;
}
If I make a breakpoint on the KeyDown function and press the Numpad keys (and every other keys) the program doesnt even comes to that breakpoint.
Do I have to change something on my Form to detect the Keys?
You'll need to set KeyPreview to true (property on the form). Also, I would advise against trying to debug the behaviour - because you may affect the behaviour you're testing (Debug.WriteLine()) is your friend here.
Just to point out that many keyboard doesnt have numpad. You can check if the key is a integer.
void Form1_KeyDown(object sender, KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar))
{
txt_string.Text += e.KeyChar;
}
}
This is more a Code Review than a solution though.

KeyPress and KeyDown events

Hello I'm trying to make a program in WF that uses the KeyPress event.
I've written the following code:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
while (true)
{
switch (e.KeyChar)
{
case (char)68:
MessageBox.Show("Test");
break;
}
}
}
But when I execute the program and press the key the message box doensn;t appear.
Does anyone have any suggestions or knows how to fix this?
I've also been told that a KeyDown event could work but I don't know how to work with those either.
Don't use while(true) in an event handler. It will loop infinitely.
Just do
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
switch (e.KeyChar)
{
case (char)68:
MessageBox.Show("Test");
break;
}
}
Also is seems cleaner to compare the pressed key to the actual character rather than the ASCII code:
switch (e.KeyChar)
{
case 'D':
MessageBox.Show("Test");
break;
}
You need to set Form.KeyPreview
e.g.
In your form
this.KeyPreview =true;

control key plus S to save

I'm trying to implement a shortcut for Ctrl+S to save in my project.
How can I acomplish this?
Here's my code:
case Keys.ControlKey:
// if control + S then save
Thanks!
I found the answer:
Control is a modifier so I need to go by the letter "G" (or whatever letter I assign to the save function) and then check whether a modifier was selected or not. Here's the code:
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.G:
if (e.Modifiers == Keys.Control)
{
//Do something
}
break;
}
}

show button being pressed when keyboard key is pressed

well i'm new to c# and i'm using Visual studio 2012.
i'm trying to make a checkbox with the appearance of a button.
when a keyboard key is pressed i would like for it to show the same way when the mouse clicks a button. If i hit the A key the button/checkbox is pressed down and if A key is hit again the button/checkbox is raised up.
i got this to work with just the button1 but i can't get it to show the pressing of the button by using this code
switch (e.KeyCode)
{
case Keys.D1:
// Simulate clicks on button1
ShowPictureButton.PerformClick();
break;
default:
break;
}
i figured i can use a checkbox so it will stay down when pressed.
If you are saying that you are using a Checkbox with it's Appearance Property set to Button you could do something like this
switch (e.KeyCode)
{
case Keys.D1:
// Simulate clicks on CheckBox's
ShowPictureButton.Checked = !ShowPictureButton.Checked;
break;
default:
break;
}
first set KeyPreview in your form properties to true
add events in your form (keypress and mouseclick) and then write your code, like this:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W)
checkBox2.Checked = !checkBox2.Checked;
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
checkBox1.Visible = !checkBox1.Visible;
}

Categories