Windows Forms: set Tab as shortcut - c#

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?

Related

How can i make a hotkey in visual c#?

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

Setting custom hotkeys

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.

Keyup not working on custom theme called "Net Seal Theme"

Today I'm having a problem with the keyup event not working on a custom theme.
public Main()
{
InitializeComponent();
tbResolveS2I.KeyUp += new KeyEventHandler(tbResolveS2I_KeyUp);
tbResolveI2S.KeyUp += new KeyEventHandler(tbResolveI2S_KeyUp);
this.KeyPreview = true;
}
private void tbResolveS2I_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
btnResolveS2I_Click(sender, e);
e.SuppressKeyPress = true;
}
}
When I hit enter on my keyboard it sends both textboxes to do the same result. What i mean is if i hit enter on the first textbox it will act like I'm pressing enter on the other textbox aswell
If you want different events to perform the same thing you could add that code to a method and just call that method from the controls event method.
Also in the theme pack you are using, the button extends from Control and does not have a PerformClick() method.
example:
public Main() {
InitializeComponent();
KeyUp += new KeyEventHandler(Main_KeyUp);
this.KeyPreview = true;
}
private void Main_KeyUp(object sender, KeyEventArgs e) {
if(e.KeyCode == Keys.Enter) {
CustomCode();
e.SuppressKeyPress = true;
}
}
private void btnResolveS2I_Click(object sender, EventArgs e) {
CustomCode();
}
private void CustomCode() {
//Perform code here
}
}

Track key press and key release on WPF applications

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.

creating shortcut key to all forms

I am planning to create a shortcut key to all my forms, I put this code to all my forms
public Form1()
{
InitializeComponent();
this.KeyDown +=new KeyEventHandler(Form1_KeyDown);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Button1.PerformClick();
}
else if (e.KeyCode == Keys.Escape)
{
button2.PerformClick();
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
}
is there any way, so that I will not repeat this codes in all my forms,
thanks :)
Use Form Base Class ... there you can write the code and inherit all the forms from that Base Form.
Raise Base Class Events in Derived Classes (C# Programming Guide)

Categories

Resources