I have a problem with key held. Everything works when it's just key down but what about key holding? The code looks like this:
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up || e.Key == Key.Down)
{
moveBall(3);
}
}
Thanks for replies.
The WPF KeyEventArgs class has an IsRepeat property which will be true if the key is being held down.
Example from the article:
// e is an instance of KeyEventArgs.
// btnIsRepeat is a Button.
if (e.IsRepeat)
{
btnIsRepeat.Background = Brushes.AliceBlue;
}
I can see two ways to do this.
The first is to continually check the Keyboard.IsKeyDown for your keys.
while (Keyboard.IsKeyDown(Key.Left) || Keyboard.IsKeyDown(Key.Right) || ...)
{
moveBall(3);
}
The second is to simply kick off your moveBall method on the KeyDown event and continue doing it until you handle a corresponding KeyUp event.
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Left || e.Key == Key.Right ...)
// think about running this on main thread
StartMove();
}
private void Window_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Left || e.Key == Key.Right ...)
// think about running this on main thread
StopMove();
}
Related
I was hoping that this code below would overwrite it, since I am assigning new stuff. But instead it executes both, selecting all and my message box
private void EventSetter_OnHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.A && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
{
System.Windows.MessageBox.Show("ctrl a");
}
}
please help thanks
If you handle the PreviewKeyDown event for the ListBox, you should be able to mark the event as handled, and the Ctrl+A should be ignored:
private void OnListBoxKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.A && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
{
e.Handled = true;
}
}
I have a simple XAML form within my WPF/C# app, containing several textboxes. I need to know the name of the control when either TAB or ENTER keys are pressed - but I don't know how to do this.
I have a function that listens for the Enter / Tab keys, but after that - I'm stumped:
public viewSearch()
{
InitializeComponent();
PreviewKeyDown += new KeyEventHandler(HandleEsc);
}
private void HandleEsc(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape) Close();
if (e.Key == Key.Enter) SearchAndDisplay();
if (e.Key == Key.Tab) SearchAndDisplay();
}
private void SearchAndDisplay()
{
MessageBox.Show("THE NAME OF THE CONTROL");
}
Thank you.
If you're looking for the control that triggers the event, you could try something as follows:
(pseudocode, since I currently have no access to Visual Studio and I can't directly check if this is all valid for WPF):
private void HandleEsc(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape) Close();
if (e.Key == Key.Enter ||
e.Key == Key.Tab) SearchAndDisplay(e.OriginalSource)
}
private void SearchAndDisplay(object sender)
{
if(sender is Control)
{
MessageBox.Show(((Control)sender).Name);
}
}
I am trying to use the KeyDown-Event (because I like to use KeyCode) to make a single action happen. For this purpose I am using a bool variable to stop continuous actions.
Can't figure out what's wrong with my code though, and haven't found a comparable problem/solution yet...
There are 2 tabs on my tabcontrol and i want to be able to switch between them using CTRL+TAB.
The switching should happen ONCE on keydown of tab.
bool tabSwitchPossible = true;
void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
tabSwitchPossible = true; //Reset boolean
}
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (tabSwitchPossible && e.KeyCode == Keys.Tab && e.Modifiers == Keys.Control)
{
tabSwitchPossible = false; //Set boolean to prevent further action
if (mainTabControl.SelectedIndex >= mainTabControl.TabCount - 1)
mainTabControl.SelectedIndex = 0;
else
mainTabControl.SelectedIndex++;
return;
}
}
Is there an automatic KeyUp event fired, even when i don't release the key?!
Thanks, in advance guys...
You don't need to handle Form1_KeyUp and tabSwitchPossible varible, remove it and just copy the following code:
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Modifiers == Keys.Control)
{
if (mainTabControl.SelectedIndex >= mainTabControl.TabCount - 1)
mainTabControl.SelectedIndex = 0;
else
mainTabControl.SelectedIndex++;
}
}
I want to call function when Ctrl+space pushed. I searched more but couldn't find what I want.
You need to add an event handler for KeyDown like: KeyDown="TextBox_KeyDown" on your TextBox.
And then in the event handler:
if (e.Key == Key.Space && e.KeyboardDevice.Modifiers == ModifierKeys.Control)
{
//Do Stuff
}
Use something like this:
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space &&
(Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
// Do what you need here
}
}
This should get you working -
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space && Keyboard.Modifiers == ModifierKeys.Control)
{
}
}
If you want to catch all the keys, whether you have the focus or not, in your class you just need to add in the constructor:
// To capture keyboard
EventManager.RegisterClassHandler(typeof(Window), Keyboard.KeyDownEvent, new System.Windows.Input.KeyEventHandler(keyDown), true);
And add the method: (it's an example, it's not for adapted for what you want)
private void keyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Space)
{
code;
}
else if ((Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) && Keyboard.IsKeyDown(Key.T))
{
code;
}
}
How do you handle a KeyDown event when the ALT key is pressed simultaneously with another key in .NET?
The KeyEventArgs class defines several properties for key modifiers - Alt is one of them and will evaluate to true if the alt key is pressed.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && e.KeyData != (Keys.RButton | Keys.ShiftKey | Keys.Alt))
{
// ...
}
}
Something like:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt)
{
e.Handled = true;
// ,,,
}
}
This is the code that finally Works
if (e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z && e.Alt){
//Do SomeThing
}
I capture the alt and down or up arrow key to increment the value of a numericUpDown control. (I use the alt key + down/up key because this form also has a datagridview and I want down/up keys to act normally on that control.)
private void frmAlzCalEdit_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && e.KeyCode == Keys.Down)
{
if (nudAlz.Value > nudAlz.Minimum) nudAlz.Value--;
}
if (e.Alt && e.KeyCode == Keys.Up)
{
if (nudAlz.Value < nudAlz.Maximum) nudAlz.Value++;
}
}
Create a KeyUp event for your Form or use a library like I did to get a GlobalHook so you can press these keys outside the form.
Example:
private void m_KeyboardHooks_KeyUp(object sender, KeyEventArgs e)
{
if ( e.KeyCode == Keys.Alt || e.KeyCode == Keys.X)
{
}
}