Is there a way to track key press and release actions on WPF?
This is what I've tried so far, but what I'm finding is that the _upDownKeyIsPressed is only set to false if I press another key - not when the up or down key is released
private void KeyUpPress(KeyEventArgs e)
{
if (e.Key == Key.Up || e.Key == Key.Down)
{
_upDownKeyIsPressed = true;
Console.WriteLine(_upDownKeyIsPressed.ToString());
}
}
private void KeyDownPress(KeyEventArgs e)
{
if (e.Key == Key.Up || e.Key == Key.Down)
{
_upDownKeyIsPressed = false;
Console.WriteLine(_upDownKeyIsPressed.ToString());
}
}
private void PlotListView_KeyUp(object sender, KeyEventArgs e)
{
KeyUpPress(e);
}
private void PlotListView_KeyDown(object sender, KeyEventArgs e)
{
KeyDownPress(e);
}
If you want to try what I said in comment. I think about something like this :
private void PlotListView_KeyUp(object sender, KeyEventArgs e)
{
TrackUpDownKeyPress(e, false);
}
private void PlotListView_KeyDown(object sender, KeyEventArgs e)
{
TrackUpDownKeyPress(e, true);
}
private void TrackUpDownKeyPress(KeyEventArgs e, bool isPressed)
{
if (e.Key == Key.Up || e.Key == Key.Down)
_upDownKeyIsPressed = isPressed;
}
If you want more info about the difference between keyUp and keyDown, check this question.
Related
So what I'm trying to do is when I press a certain key, it will send keys. This is what I have so far. When I click F1 it doesn't do anything, and I'm not sure why.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F1)
{
SendKeys.Send("Example Text");
}
}
Try :
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 is pressed");
}
}
There is no reaction on Tab shortcut in my app:
public class SomeWindow : Form {
// ...
private void someWindow_Load(object sender, EventArgs e) {
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(SomeWindow_KeyDown);
}
void SomeWindow_KeyDown(object sender, KeyEventArgs e) {
if (e.Control && e.KeyCode == Keys.B) {
// some actions
}
else if (e.KeyCode == Keys.Tab) {
// no reaction
}
}
}
I learned that Tab and direction keys are required some overrides, but all examples that I saw was related to textboxes. Any solutions for Tas as shortcut?
private void timer3_Tick(object sender, EventArgs e)
{
if (GetAsyncKeyState(Keys.LShiftKey) == -32767)
{
timer2.Start();
}
if (GetAsyncKeyState(Keys.LControlKey) == -32767)
{
timer2.Stop();
}
}
I use this for hot keys but is there a way for me to have a set button so a user could pick and set his own hotkey in the program instead of using a preset one.
I am trying to change a Boolean value if a key is pressed down, but when released revert back to false so I can only trigger a if statement if all the arguments a re correct and a key is pressed down.
public class boolean
{
public static Boolean keyPresed = false;
}
private void mainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.A)
{
MessageBox.Show(" just for debugging ");
boolean.keyPresed = true;
}
else {
boolean.keyPressed = false;
}
if (tileBuyPlayer2.Checked && ownerShipsPlayer2.peacockOwner == false && boolean.keyPresed == true)
{
// do this sorry not on here cause how big it is
}
}
}
You can declare keyAPressed as a global var in this class, and reset it in your KeyDown event:
public class DemoClass
{
public static Boolean keyAPressed = false;
private void mainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
{
MessageBox.Show(" just for debugging ");
keyAPressed = true;
}
else
{
keyAPressed = false;
}
if (keyAPressed == true)
{
// Reset keyAPressed to false
keyAPressed = false
if (tileBuyPlayer2.Checked && ownerShipsPlayer2.peacockOwner == false)
{
// do this sorry not on here cause how big it is
}
}
}
}
or use KeyUp event to reset keyAPressed:
public class DemoClass
{
public static Boolean keyAPressed = false;
private void mainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
{
MessageBox.Show(" just for debugging ");
keyAPressed = true;
}
else
{
keyAPressed = false;
}
if (tileBuyPlayer2.Checked && ownerShipsPlayer2.peacockOwner == false && keyAPressed == true)
{
// do this sorry not on here cause how big it is
}
}
private void mainForm_KeyUp(object sender, KeyEventArgs e)
{
keyAPressed = false;
}
}
If you are happy to try Microsoft's reactive framework then you could try doing something like this:
var keyDown = Observable.FromEventPattern<KeyEventArgs>(mainForm, "KeyDown");
var query =
from kd in keyDown
where kd.EventArgs.KeyCode == Keys.A
where tileBuyPlayer2.Checked
where ownerShipsPlayer2.peacockOwner == false
select kd;
query.Subscribe(x =>
{
// do your code here
});
I might have missed something that you're trying to do, but I think that makes things much easier.
I've one button - button1click and textbox in which when i type something and press enter, i'd like to run code from button1click.
How can I do it without copying entire code from button1click into EnterPressed?
private void button1click(object sender, EventArgs e)
{
//Some Code
}
void EnterPressed(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Execute code from button1
}
}
Maybe something like that? But I'm getting errors...
void EnterPressed(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1click(object sender, EventArgs e)
}
}
Just have button1_click call a method and then you can call that same method from anywhere you want to. So in your example:
private void button1click(object sender, EventArgs e)
{
Foo();
}
void EnterPressed(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Foo();
}
}
void Foo()
{
//Do Something
}
I personally wouldn't manually call another control's event.
void EnterPressed(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1click(sender, EventArgs.Empty);
}
}