How can I handle hardware keys in my application?

By Vassili Philippov, September 11, 2001.

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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值