Thursday
3
Apr 2003

LaunchBar and .NET

(11:19 pm) Tags: [General]

In attempting to keep the skills sharp, I am writing a program in .NET to emulate the functionality of LaunchBar for Mac OS X.

In doing so, I have found that I still have to delve into the Win32 API to get some work done. In that respect, there is still a ways to go. An example is mapping a hotkey to your application. Since Alex gets the pleasure of working on both Windows and Mac, I wanted to make my launcher as much like LaunchBar as possible. The first step is the way you launch it. On the Mac, you press Command + Space, so I need to do the same on Windows with WindowsKey + Space. So I dig into finding out how to do this. After a few hours of Googling around, I come up with the proper sequence:

  1. Call the GlobalAddAtom() function, to get a quasi-unique integer for yourself.
  2. Call the RegisterHotKey() function, passing your form’s handle (so that Windows will activate your app if it is not currently active), this integer from the previous call, what key you would like to register, and the modifiers on it.

Then, when your application is about to exit, you need to clean these up:

  1. Call the UnregisterHotKey() function
  2. Call the GlobalDeleteAtom(), releasing your integer back to the OS

What this maps to in .NET:

//Register the functions that you want to use
	
[DllImport(”kernel32″)]
private static extern int GlobalAddAtom(String atomString);
	
[DllImport(”kernel32″)]
private static extern int GlobalDeleteAtom(int atom);
	
[DllImport(”user32″)]
private static extern int RegisterHotKey(int hWnd, int id, uint modifiers, uint vk);
	
[DllImport(”user32″)]
private static extern int UnregisterHotKey(int hWnd, int id);
	
//Then call the functions to register the hot key
int atom = GlobalAddAtom(”WIN+SPACE”);
//0×8 is the flag for the Windows Key modifier, and 0×20 is the space key
int result = RegisterHotKey(Form.Handle.ToInt32(), atom, 0×8, 0×20);
	
//Then call the functions to un-register the hot key when you are about to shut down.
UnregisterHotKey(Form.Handle.ToInt32(), atom);
GlobalDeleteAtom(atom);
	

Popularity: 8%

Comments: (0)