News | Articles | Libraries | Developer Tools | Books | Forum Links | Search   
Sections:
 

QA: How can I handle hardware keys in my application?

By Vassili Philippov, September 11, 2001.
Print version

Question

By default pressing hardware keys launches other applications (such as Contacts, Calendar, etc). I need to handle hardware keys in my program. How can I do it?

Answer

You should register your window to handle hardware keys. Documentation says that you have to call RegisterHotKey function for that. But it is not enough. You should also call undocumented function UnregisterFunc1 defined in coredll.dll. After that you could handle pressing hardware keys by handling WM_KEYUP event.

Here is a code that loads UnregisterFunc1 function from coredll.dll library:

typedef BOOL (__stdcall *UnregisterFunc1Proc)( UINT, UINT ); HINSTANCE hCoreDll; UnregisterFunc1Proc procUndergisterFunc; hCoreDll = LoadLibrary(_T("coredll.dll")); if (hCoreDll) { procUndergisterFunc = (UnregisterFunc1Proc)GetProcAddress( hCoreDll, _T("UnregisterFunc1")); }

CSTUtil

STUtil library encapsulates working with this undocumented function. Just use RegisterHotKey method of CSTUtil class that works like standard RegisterHotKey WinAPI function but also calls necessary undocumented function.

Sample application

You could download sample application (52 Kb) that handles all hardware keys and shows a message box when a key is pressed.

Sample code

Using STUtil library

Download STUtil library. Here is a sample code that registers a window to handle all hardware buttons:

CSTUtil su; for (int i=0xc1; i<=0xcf; i++) { su.RegisterHotKey(m_hWnd, i, i); }

Without STUtil library

Here is a sample code that registers a window to handle all hardware buttons without using STUtil library:

typedef BOOL (__stdcall *UnregisterFunc1Proc)( UINT, UINT ); HINSTANCE hCoreDll; UnregisterFunc1Proc procUndergisterFunc; hCoreDll = LoadLibrary(_T("coredll.dll")); ASSERT(hCoreDll); procUndergisterFunc = (UnregisterFunc1Proc)GetProcAddress( hCoreDll, _T("UnregisterFunc1")); ASSERT(procUndergisterFunc); for (int i=0xc1; i<=0xcf; i++) { procUndergisterFunc(MOD_WIN, i); RegisterHotKey(hWnd, i, MOD_WIN, i); } FreeLibrary(hCoreDll);

Related resources:

Discuss

Discuss this article. Here you can write your comments and read comments of other developers.
Rate this article:     Poor Excellent    
 12345 
© 2001-2005 Pocket PC Developer Network, a division of Spb Software House