Help me about Code C# hotkey

Posts 18 of 8 · Page 1 of 1
Help me about Code C# hotkey
Help me about Code C#
I want code c#
Examble

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F5)
{

timer1.Start();



>>>>i want code is Press F5 time1.Start
press F5 again >>> timer1.Stop
Help me Plese
Quote Originally Posted by it-ty View Post
Help me about Code C#
I want code c#
Examble

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F5)
{

timer1.Start();



>>>>i want code is Press F5 time1.Start
press F5 again >>> timer1.Stop
Help me Plese
This would mean that the user would have the From1 active. Else the KeyDown won't be registered.


Many people uses:
Code:
[DllImport("user32.dll")]
static extern bool GetAsyncKeyState(System.Windows.Forms.Keys vKey);
To check if a key is pressed, within a timer.

Like: (Timer_Tick() is just because I don't remember the object and args)
Code:
Timer_Tick()
{
if(GetAsyncKeyState(Keys.F5))
     Timer1.Start();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F5)
{

timer1.Start();
}
else
{
timer1.Stop();
}
}
^^^^Code not work
I want code is press F5 first >>timer1.Start();
press F5 again >>>timer1.Stop();
Quote Originally Posted by Jorndel View Post


This would mean that the user would have the From1 active. Else the KeyDown won't be registered.


Many people uses:
Code:
[DllImport("user32.dll")]
static extern bool GetAsyncKeyState(System.Windows.Forms.Keys vKey);
To check if a key is pressed, within a timer.

Like: (Timer_Tick() is just because I don't remember the object and args)
Code:
Timer_Tick()
{
if(GetAsyncKeyState(Keys.F5))
     Timer1.Start();
}
I'd use a low level keyboard hook instead. But this would also work (you do need a higher interval between ticks).
Code:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
   if (e.KeyCode == Keys.F5)
   {
       if (timer1.Enabled)
           timer1.Stop();
       else
           timer1.Start();
   }
}
Quote Originally Posted by Biesi View Post
Code:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
   if (e.KeyCode == Keys.F5)
   {
       if (timer1.Enabled)
           timer1.Stop();
       else
           timer1.Start();
   }
}
Probably the easiest way...
Quote Originally Posted by Biesi View Post
Code:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
   if (e.KeyCode == Keys.F5)
   {
       if (timer1.Enabled)
           timer1.Stop();
       else
           timer1.Start();
   }
}
Wow! very Good Nice! Thank you
and I mod. new code is
if (e.KeyCode == Keys.F5)
{
if (timer1.Enabled)
{
label1.Text = "Off";
timer1.Stop();
}
else
{
timer1.Start();
label1.Text = "ON";
}
}
}

Thank you "Biesi"
Looks like you've already got the solution, but I have nothing better to do so I'll just contribute to this thread.

Code:
[System.Runtime.InteropServices.DllImport("User32.DLL")]
private static extern bool GetAsyncKeyState(Keys key);

private static System.Windows.Forms.Timer hotkeyTimer = new System.Windows.Forms.Timer();

public Main()
{
    hotkeyTimer.Tick += new EvenHandler(hotkeyTimer_Tick);
    hotkeyTimer.Interval = 1;
    hotkeyTimer.Start();
}

public void hotkeyTimer_Tick(System.Object sender, System.EventArgs e)
{
    if (GetAsyncKeyState(Keys.A))
    {
        //Do something when the A key is pressed
    }
}
Posts 18 of 8 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?