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;
}
}
Related
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();
}
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 textbox with a OnKeyPress event. In this textbox I wish to input only numbers, and for some specific letters like t or m, I would want to execute a code without that letter being typed in the textbox. Small sample of what I am trying to do:
//OnKeyPressed:
void TextBox1KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.T || e.KeyCode == Keys.M) Button1Click(this, EventArgs.Empty);
}
This unfortunately does not prevent the input of the letter..
Set the SuppressKeyPress property from KeyEventArgs to true, like below:
private void TextBox1KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.T || e.KeyCode == Keys.M)
{
e.SuppressKeyPress = true;
Button1Click(this, EventArgs.Empty);
}
}
You could always run the TryParse on the keyDown event so as to validate as the data gets entered. It saves the user an additional UI interaction.
private void TextBox1KeyDown(object sender, KeyEventArgs e)
{
int i;
string s = string.Empty;
s += (char)e.KeyValue;
if (!(int.TryParse(s, out i)))
{
e.SuppressKeyPress = true;
}
else if(e.KeyCode == Keys.T || e.KeyCode == Keys.M)
{
e.SuppressKeyPress = true;
Button1Click(this, EventArgs.Empty);
}
}
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++;
}
}