блакбазык
блакбазык
CC#
Created by блакбазык on 5/14/2023 in #help
❔ 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 :/
13 replies