❔ WinForm - Trigger a function when the program does not have focus when the user press TAB

Hi. I'm very new to C# and WinForms, I'm actually a Dart developer. I'm trying to create some sort of stopwatch, where the program will always be on top of all the other programs, but will not have focus, and when the user press TAB it will start or reset the stopwatch. Here's what I found during my research: I have found a code that apparently fetch my tab key even if the program does not have focus.
[DllImport("User32.dll")]
static extern short GetAsyncKeyState(Int32 vKey);

int VK_TAB = 0x09;


short keyStatus = GetAsyncKeyState(VK_TAB);

if ((keyStatus & 1) == 1)
{
MessageBox.Show("Start the stopwatch");
}
[DllImport("User32.dll")]
static extern short GetAsyncKeyState(Int32 vKey);

int VK_TAB = 0x09;


short keyStatus = GetAsyncKeyState(VK_TAB);

if ((keyStatus & 1) == 1)
{
MessageBox.Show("Start the stopwatch");
}
But I do not have any idea of where to put it in my existing code.
public Form1()
{

InitializeComponent();
Resize += Form1_Resize;
Load += Form1_Load;
HOTKEY_ACTIVE += Start;
FormClosing += Form1_FormClosing;




//I tried to put the TAB fetching code here
}
public Form1()
{

InitializeComponent();
Resize += Form1_Resize;
Load += Form1_Load;
HOTKEY_ACTIVE += Start;
FormClosing += Form1_FormClosing;




//I tried to put the TAB fetching code here
}
But I do not have any result :/
7 Replies
phaseshift
phaseshift15mo ago
I don't think you want that code. Its just a check to see if tab key is pressed when you run that code
блакбазык
Oh
Buddy
Buddy15mo ago
You can use RegisterHotKey (https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerhotkey) I guess Although TAB is definitely a weird hotkey and probably reserved?
блакбазык
I have some override to detect the tabkey:
void Form1_KeyDown(object sender, KeyEventArgs e)
{


MessageBox.Show(e.KeyCode.ToString());
if (e.KeyCode == Keys.Tab)
{

MessageBox.Show("OH!");
}

}

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Tab)
{
object sender = Control.FromHandle(msg.HWnd);
KeyEventArgs e = new KeyEventArgs(keyData);
Form1_KeyDown(sender, e);
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
void Form1_KeyDown(object sender, KeyEventArgs e)
{


MessageBox.Show(e.KeyCode.ToString());
if (e.KeyCode == Keys.Tab)
{

MessageBox.Show("OH!");
}

}

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Tab)
{
object sender = Control.FromHandle(msg.HWnd);
KeyEventArgs e = new KeyEventArgs(keyData);
Form1_KeyDown(sender, e);
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
@LLVM Do I have to import user32.dll to use the hotkeys ?
Buddy
Buddy15mo ago
RegisterHotKey? Yes.
блакбазык
Where should I use the RegisterHotKey(Handle, 1, HotKey.FsModifiers.None, Keys.Tab); ? Should I just put it directly in the main ? Ok, it seem that I can put it as soon as I have a the windows Handle, so I put it here:
public Form1()
{
InitializeComponent();
Resize += Form1_Resize;
Load += Form1_Load;
HOTKEY_ACTIVE += Start;
FormClosing += Form1_FormClosing;


Api.RegisterHotKey(Handle, 1, HotKey.FsModifiers.None, Keys.Tab);
}
public Form1()
{
InitializeComponent();
Resize += Form1_Resize;
Load += Form1_Load;
HOTKEY_ACTIVE += Start;
FormClosing += Form1_FormClosing;


Api.RegisterHotKey(Handle, 1, HotKey.FsModifiers.None, Keys.Tab);
}
Now, how I can call a function when the hotkey is being pressed ?
Accord
Accord14mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.