KeyPress and KeyDown events - c#

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;

Related

How to get a clickable button to also activate upon a keypress? (Windows Form C#)

I have a regular button called Btn_Down that activates when clicked, but I also want it to activate when the 'S' key is pressed. Can anyone help me out with this?
Subscribe to KeyDown event of form control which contains your button and add following code
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyData)
{
case Keys.S:
button1_Click(this, EventArgs.Empty);
//Or
//button1.PerformClick();
break;
}
}
Set form's KeyPreview property to true. These settings should give the effect you want.
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
switch (e.KeyChar)
{
case 'S':
case 's':
button1_Click(this, EventArgs.Empty);
//Or
//button1.PerformClick();
break;
}
}

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

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;
}

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.

C# key pressed listener in a Windows Forms UserControl

Good day,
I want to add key listeners in a forms user control. Which is included in a main form.
When a user presses a key in my application I want it to do some thing.
How do I go about doing this?
for example. If I pressed w then a popup would appear saying you pressed w.
I have tried this in my User Control Class;
private void UC_ControlsTank_KeyPress(object sender, KeyPressEventArgs e)
{
Console.Write("You pressed:" + e.KeyChar);
}
But when i press keys nothing seems to happen.
EDIT:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show(e.KeyChar.ToString());
}
}
Here is my form:
and here is how I linked it in visual studio:
First of all before capturing KeyPress you have to set the KeyPreview property of the Win Form to true. Unless you set it to true KeyPress will not be captured.
There is a KeyPressed event that you can use
Control.KeyPress Event
This has System.Windows.Forms.KeyEventArgs e that will tell you what key was pressed
Edit:
So I just tried this on a sample app and it worked fine
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show(e.KeyChar.ToString());
}
Here is an Example ,
private void control_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyData)
{
case Keys.W:
{
MessageBox.Show("you pressed w");
break;
}
case Keys.B:
{
MessageBox.Show("you pressed b");
break;
}
case Keys.F11:
{
MessageBox.Show("you pressed F11");
break;
}
case Keys.Escape:
{
this.Close();
break;
}
default:
{
break;
}
}
}
or
Reference this Overriding Keydown in a User Control using ProcessKeyPreview .

How to record typing in C# (keyListener)

I want to write a simple text to speech program.
First, I want to make the program play only the written symbol. For example, if I type 'a' I want the program to say 'a' (I have recorded all of them), so when I type a word, it should spell it.
However, I am a beginner in C# and .Net and don't how to make the program understand the text I type. For example, in java I heard that there is a keyListener class, but I don't know which class should I use. I looked on MSDN but couldn't find it.
Which class or function should I use to listen to typed keys?
I suppose you are planning to use Windows Forms to achieve this.
The solution would be pretty simple. These events include MouseDown, MouseUp, MouseMove, MouseEnter, MouseLeave, MouseHover, KeyPress, KeyDown, and KeyUp. Each control has these events exposed. You just need to subscribe to it.
Please refer to this
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown.aspx
There would be a little bit of logic to find whether a complete word has been typed or not. A simple soultion would be , when space has been pressed, you can assume a word has been completed. Its very crude logic, as the user may have typed in wrong spelling and want hit backspace and correct the spelling. You may want to add lag to it.
If you are using Visual Studio like every other C# developer here is a more detailed code example:
Create a Windows Form and go to the [Design].
Select its properties (RMB=>properties), navigate to Events and double click LMB on KeyDown
VS will create and bind the event for you
Handle the KeyEventArgs depending on its value.
Example:
private void NewDialog_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyData)
{
case Keys.A:
{
MethodToOutputSound(AEnum);
break;
}
case Keys.B:
{
MethodToOutputSound(BEnum);
break;
}
case Keys.F11:
{
DifferentMethod();
break;
}
case Keys.Escape:
{
this.Close();
break;
}
default:
{
break;
}
}
}
Or use a lot of ifs
private void NewDialog_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyData == Keys.A)
{
MethodToOutputSound(AEnum);
}
if(e.KeyData == Keys.B)
{
MethodToOutputSound(BEnum);
}
...
}
Create a Windows Form with a TextBox in it. Handle the KeyPress event - that will give you the actual character that the user types. KeyDown and KeyUp won't help you.
You need to check the KeyChar property. Like this:
void MyEventHandler(object sender, KeyPressEventArgs e) {
// Do stuff depending on the value of e.KeyChar
}
private void button1_Click_1(object sender, EventArgs e)
{
string word = textBox1.Text;
foreach (char i in word)
{
switch (i)
{
case 'a':
case 'A': { // play sound a
break;
}
default:
{
// play no sound
break;
}
}
}
}

Categories