Global hotkeys help

Making hotkeys to C# Form

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 5449 Views - Last Post: 03 May 2009 - 06:59 AM Rate Topic: -----

#1 Programmerz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-April 09

Global hotkeys help

Post icon  Posted 29 April 2009 - 02:50 AM

First I know C# But I'm new to C# Forms, theres some commands I don't know in forms, I'm new to C# forms.

I started making an Auto clicker, with some new commands that I needed and used google to find, I finished it but its missing one thing..Hotkeys.

I need to make two hotkeys like F1 to start and F2 to stop, I didn't find any tutorial/commands how to make this.

Please teach me how can I make it alone (Tutorial), or link me a tutorial if you find.
I can't ask for a code tho..


Thank you :)

Here is the current code if needed:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MyFirstAutoClicker
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{

		}

		private void StartAC_Click(object sender, EventArgs e)
		{
			timer1.Start();
		}

		private void StopAC_Click(object sender, EventArgs e)
		{
			timer1.Stop();
		}

		private void CountUp_Click(object sender, EventArgs e)
		{
			int x = int.Parse(CountLabel.Text);
			if (x < 1000)
			{
				x++;
				string newstr = x.ToString();
				CountLabel.Text = newstr;
			}
		}

		private void ResetCounter_Click(object sender, EventArgs e)
		{
			CountLabel.Text = "0";
		}

		private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
		private const int MOUSEEVENTF_LEFTUP = 0x0004;

		[DllImport("user32.dll", CharSet = CharSet.Auto,CallingConvention = CallingConvention.StdCall)]
		public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons,
		int dwExtraInfo);

		private void timer1_Tick(object sender, EventArgs e)
		{
			mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
			mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
		}
	}
}



This post has been edited by Programmerz: 29 April 2009 - 02:53 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Global hotkeys help

#2 b.ihde  Icon User is offline

  • D.I.C for life
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,070
  • Joined: 29-September 08

Re: Global hotkeys help

Posted 29 April 2009 - 03:02 AM

Hi!

To use Keyevents you have to use KeyEventArgs instead of Eventargs..

Look at this link to see how to use these KeyEventArgs ;) -> Link

Hope that helps!


regards

Ben
Was This Post Helpful? 1
  • +
  • -

#3 Programmerz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-April 09

Re: Global hotkeys help

Posted 29 April 2009 - 05:46 AM

Do you mean I have to add this in the public partial class Form1 : Form?

		private void Form1_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.F1)
			{
				timer1.Start();
			}

			if (e.KeyCode == Keys.F2)
			{
				timer1.Stop();
			}
		}



If so..It didn't work :\
Was This Post Helpful? 0
  • +
  • -

#4 Programmerz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-April 09

Re: Global hotkeys help

Posted 29 April 2009 - 07:36 AM

Hey I found this tutorial but didn't understand anything..
If you can help me making hotkeys like
<<if pressed F1 do timer1.Start(); and F2 do timer1.Stop();>>
with this tutorial:

http://www.dotnet2th...a...a78&Lang=cs

I didn't understand any but I need those hotkeys >_<.

Thank you.
Was This Post Helpful? 0
  • +
  • -

#5 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 62
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Global hotkeys help

Posted 29 April 2009 - 07:42 AM

To elaborate a bit on your problem, you want to launch your application and then in any place of your PC your program listens to the F1 and F2 keys? Doesn't matter if your program has focus or not.

Is this correct?
Was This Post Helpful? 0
  • +
  • -

#6 InternalStatic  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 11
  • Joined: 28-April 09

Re: Global hotkeys help

Posted 29 April 2009 - 07:48 AM

View PostProgrammerz, on 29 Apr, 2009 - 04:46 AM, said:

Do you mean I have to add this in the public partial class Form1 : Form?

		private void Form1_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.F1)
			{
				timer1.Start();
			}

			if (e.KeyCode == Keys.F2)
			{
				timer1.Stop();
			}
		}



If so..It didn't work :\


Did you remember to put this.KeyDown += Form1_KeyDown first? If you don't subscribe the event, the form doesn't listen for it. However, this will only work if the form is active--if you need it to work otherwise, that involves some more Win32 API calls and such, I believe.
Was This Post Helpful? 0
  • +
  • -

#7 Programmerz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-April 09

Re: Global hotkeys help

Posted 29 April 2009 - 10:24 AM

View Postpapuccino1, on 29 Apr, 2009 - 06:42 AM, said:

To elaborate a bit on your problem, you want to launch your application and then in any place of your PC your program listens to the F1 and F2 keys? Doesn't matter if your program has focus or not.

Is this correct?

Exactly.
___________________________________________________________________


View PostInternalStatic, on 29 Apr, 2009 - 06:48 AM, said:

View PostProgrammerz, on 29 Apr, 2009 - 04:46 AM, said:

Do you mean I have to add this in the public partial class Form1 : Form?

		private void Form1_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.F1)
			{
				timer1.Start();
			}

			if (e.KeyCode == Keys.F2)
			{
				timer1.Stop();
			}
		}



If so..It didn't work :\


Did you remember to put this.KeyDown += Form1_KeyDown first? If you don't subscribe the event, the form doesn't listen for it. However, this will only work if the form is active--if you need it to work otherwise, that involves some more Win32 API calls and such, I believe.

No, I didn't put this.KeyDown += Form1_KeyDown.
___________________________________________________________________


View PostInternalStatic, on 29 Apr, 2009 - 06:48 AM, said:

However, this will only work if the form is active

I need the hotkeys to work even if I'm not focused in the application.
___________________________________________________________________


View PostInternalStatic, on 29 Apr, 2009 - 06:48 AM, said:

otherwise, that involves some more Win32 API calls and such, I believe.

How can I use that? :)
Was This Post Helpful? 0
  • +
  • -

#8 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 942
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Global hotkeys help

Posted 29 April 2009 - 10:33 AM

View PostProgrammerz, on 29 Apr, 2009 - 01:50 AM, said:

First I know C# But I'm new to C# Forms, theres some commands I don't know in forms, I'm new to C# forms.

I started making an Auto clicker, with some new commands that I needed and used google to find, I finished it but its missing one thing..Hotkeys.

I need to make two hotkeys like F1 to start and F2 to stop, I didn't find any tutorial/commands how to make this.

Please teach me how can I make it alone (Tutorial), or link me a tutorial if you find.
I can't ask for a code tho..


Thank you :)

Here is the current code if needed:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MyFirstAutoClicker
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{

		}

		private void StartAC_Click(object sender, EventArgs e)
		{
			timer1.Start();
		}

		private void StopAC_Click(object sender, EventArgs e)
		{
			timer1.Stop();
		}

		private void CountUp_Click(object sender, EventArgs e)
		{
			int x = int.Parse(CountLabel.Text);
			if (x < 1000)
			{
				x++;
				string newstr = x.ToString();
				CountLabel.Text = newstr;
			}
		}

		private void ResetCounter_Click(object sender, EventArgs e)
		{
			CountLabel.Text = "0";
		}

		private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
		private const int MOUSEEVENTF_LEFTUP = 0x0004;

		[DllImport("user32.dll", CharSet = CharSet.Auto,CallingConvention = CallingConvention.StdCall)]
		public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons,
		int dwExtraInfo);

		private void timer1_Tick(object sender, EventArgs e)
		{
			mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
			mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
		}
	}
}




F1 would be a bad choice for a hotkey as it usually invokes the help function in applications like Visual Studio. That actually goes for all of the F keys in other applications.

Can't think of a good choice off the top of my head. You could use the same key. Like <Ctrl><Shift>S to start and stop the timer and have a variable in the form to hold the state of the timer.
Was This Post Helpful? 0
  • +
  • -

#9 Programmerz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-April 09

Re: Global hotkeys help

Posted 29 April 2009 - 10:53 AM

View PostSixOfEleven, on 29 Apr, 2009 - 09:33 AM, said:

F1 would be a bad choice for a hotkey as it usually invokes the help function in applications like Visual Studio. That actually goes for all of the F keys in other applications.

Can't think of a good choice off the top of my head. You could use the same key. Like <Ctrl><Shift>S to start and stop the timer and have a variable in the form to hold the state of the timer.


Yeah I don't care what are the hotkeys but I need them to work, And without focusing in the application

Thank you.
Was This Post Helpful? 0
  • +
  • -

#10 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 942
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Global hotkeys help

Posted 29 April 2009 - 03:45 PM

You might want to look into a tutorial that will minimize an application to the notification tray. That would be a good place to start.
Was This Post Helpful? 0
  • +
  • -

#11 Programmerz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-April 09

Re: Global hotkeys help

Posted 29 April 2009 - 09:01 PM

View PostSixOfEleven, on 29 Apr, 2009 - 02:45 PM, said:

You might want to look into a tutorial that will minimize an application to the notification tray. That would be a good place to start.

I'll learn more about minimizing the application to tray, But don't I need the hotkeys to work first?


Anybody got them to work or something?
Still didn't find anything >_<
Was This Post Helpful? 0
  • +
  • -

#12 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 942
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Global hotkeys help

Posted 29 April 2009 - 09:22 PM

I'll try and look into it but I can't guarantee anything as I've never tried it before.
Was This Post Helpful? 0
  • +
  • -

#13 b.ihde  Icon User is offline

  • D.I.C for life
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,070
  • Joined: 29-September 08

Re: Global hotkeys help

Posted 29 April 2009 - 11:46 PM

HI again, did you set on your form the KeyPreview property to true?? This will allow the form to see all key events first even when a control on the form has focus.

regards

Ben

EDIT: The code i found here is ok, it should work..

This post has been edited by b.ihde: 29 April 2009 - 11:48 PM

Was This Post Helpful? 0
  • +
  • -

#14 Programmerz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-April 09

Re: Global hotkeys help

Posted 30 April 2009 - 05:14 AM

View Postb.ihde, on 29 Apr, 2009 - 10:46 PM, said:

HI again, did you set on your form the KeyPreview property to true?? This will allow the form to see all key events first even when a control on the form has focus.

regards

Ben

EDIT: The code i found here is ok, it should work..


Do you mean Key preview True with this code?
		private void Form1_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.F1)
			{
				timer1.Start();
			}

			if (e.KeyCode == Keys.F2)
			{
				timer1.Stop();
			}
		}



I set Key preview to true but which code do I need then?
Was This Post Helpful? 0
  • +
  • -

#15 b.ihde  Icon User is offline

  • D.I.C for life
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,070
  • Joined: 29-September 08

Re: Global hotkeys help

Posted 30 April 2009 - 05:25 AM

No that code should be correct..

I ll send you a screenshot later this day, when i m at home.. about 2 hours from now :)
In the DesignerView look at the property explorer.. I donīt know exactly where atm..

Ben
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2