I have no idea how do this.
I know only how do detect one key:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.C)
{
MessageBox.Show("C key");
}
}
You have to keep track of keydown/keyup events, and keep a list of all the keys that are currently "down". The keyboard handler can only trigger on individual keys, and it's up to your code to detect/keep track of which ones are down, and if those individual keydown events are close enough to each other to be counted as "together".
put a break point in your key down event and press your two keys together. examine the KeyData of the KeyEventArgs. it will show you what you have to use to detect two keys pressed together. Use some dummy code like this:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("KeyData is: " + e.KeyData.Tostring());
}
like I have done for shift and r pressed together
As you can see, you can use a timer event with booleans to detect if two keys are pressed:
bool keyup = false;
bool keyleft = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
keyup = true;
}
else if (e.KeyCode == Keys.Left)
{
keyleft = true;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
keyup = false;
}
else if (e.KeyCode == Keys.Left)
{
keyleft = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (keyleft && keyup)
{
Console.Beep(234, 589);
}
}
Use this:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
string keysPressed = keyData.ToString();
// your code goes here
}
This is what I get for Up + Shift: "Up, Shift"
Related
Is an easy way to cancel click event when user hit enter on button (instead of mouse click on button?)
i have tried with:
private void button3_Click(object sender, EventArgs e)
{
KeyEventArgs ke = e as KeyEventArgs;
if (ke != null)
{
if (ke.KeyCode == Keys.Enter)
{
return;
}
}
}
But ke is null
public void btnClick(object sender, EventArgs e)
{
bool IsMouse = (e is System.Windows.Forms.MouseEventArgs);
// If not mouse, they hit spacebar or enter
}
Yes it will be null. because EventArgs is not KeyEventArgs
KeyEventArgs will be passed as a parameter to KeyDown or KeyUp events. You're messing up things.
You can do something like this
private bool flag = false;
private void button1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
flag = true;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (flag)
{
flag = false;
return;
}
//else do original task
}
You can handle KeyPress event for the button and disable or ignore enter key there instead of returning in button click
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ((keyData & Keys.KeyCode) == Keys.Enter)
{
SendKeys.Send("{Tab}");
return true;
}
return false;
}
I'm designing a device app. Compact Framework 2.0
I want the user to press F1 to navigate to the next screen, but it does not work.
Can't seem to find a solution.
Is it possible?
This is how I normally use Keypress:
This works:
if (e.KeyChar == (char)Keys.M)
{
MessageBox.Show("M pressed");
e.Handled = true;
}
This dos NOT work:
if (e.KeyChar == (char)Keys.F1)
{
MessageBox.Show("F1 pressed");
e.Handled = true;
}
try this
private void Form1_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
}
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.ToString() == "F1")
{
MessageBox.Show("F1 pressed");
}
}
Refer This
You can override the ProcessCmdKey method of your form class and use keyData == Keys.F1 to check whether F1 is pressed. Above link has example as follows.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.F1)
{
MessageBox.Show("You pressed the F1 key");
return true; // indicate that you handled this keystroke
}
// Call the base class
return base.ProcessCmdKey(ref msg, keyData)
}
Some keys(like f1,f2,arrow keys ,tab ....)cannot be "captured" by keychar for that you need to use keycode:
if (e.KeyCode == Keys.F1)
{
// do stuff
}
keychar property - http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.keychar.aspx
Use KeyDown instead of KeyPress:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.F1)
{
// your code here
}
}
Also set KeyPreview to true
Let's say I've got this:
private void txtAnalogValue_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//Non-numeric key pressed => prevent this from being input into the Textbox
e.SuppressKeyPress = true;
}
}
and this:
private void txtAnalogValue_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
try
{
UpdateState(double.Parse(((TextBox)sender).Text));
}
catch (Exception ex)
{
((TextBox)sender).Text = ioElement.StateVal.ToString("0.00");
}
}
}
I know this code doesn't make a lot of sense, it's just test.
The question is: Will the e.SuppressKeyPress = true in KeyDown event have affect on KeyUp event, so the Enter key will not be accepted?
No, e.SuppressKeyPress = true will just ignore the Enter key (it won't go to the next line and Text property of the textbox won't be changed) and e.Keycode will be visible in the KeyUp. Therefore suppressing the key in the KeyDown doesn't affect the KeyUp event and your code should work. The UpdateState will be called when you hit Enter button in the TextBox. You can try this code to check it:
private void txtAnalogValue_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
{
e.SuppressKeyPress = true;
}
}
private void txtAnalogValue_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
{
MessageBox.Show("Up");
}
}
If you press and hold the 5 key on the numpad it will continue to execute a statement in the KeyDown event handler. How can i ensure the statement is executed only once, even if i hold the key down?
Thanks for your attention.
private void form_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.NumPad5)
{
dados.enviar("f"); //I want this to run only once!
}
}
You can set flag on key down and reset it on key up.
private bool isPressed = false;
public Form1()
{
InitializeComponent();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.B && !isPressed )
{
isPressed = true;
// do work
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (isPressed )
isPressed = false;
}
bool alreadyPressed = false;
...
if (e.KeyCode == Keys.NumPad5 && ! alreadyPressed)
{
alreadyPressed = true;
...
You should use IsRepeat flag to check if it's the first time key is pressed.
if (e.KeyCode == Keys.NumPad5 && !e.IsRepeat)
I have a standard WinForms-application. I want to implement such functionality:
user can press and hold only one keyboard button at a time. If he tried to press a button, while another button pressed, then it gets no result.
PS: this behavior spreads only to a form that I want, not to all forms of my application.
C#, 2.0 - 3.5, VS 2008
I got something similar than Khadaji
private Keys CurrentKey = Keys.None;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (CurrentKey == Keys.None)
{
CurrentKey = e.KeyData;
// TODO: put your key trigger here
}
else
{
e.SuppressKeyPress = true;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == CurrentKey)
{
// TODO: put you key end trigger here
CurrentKey = Keys.None;
}
else
{
e.SuppressKeyPress = true;
}
}
I banged this out pretty quickly, so you might have to tinker with it to make it work, but it should get you started.
Set your form's KeyPreview to true. Put the in a KeyDown event and KeyUp Event.
Keys MyKey;
bool KeyIsDown = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (KeyIsDown)
{
e.Handled = true;
}
else
{
MyKey = e.KeyData;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (KeyIsDown)
{
if (e.KeyData == MyKey)
{
KeyIsDown = false;
}
}
}