How Can I Handle Hardware Keys in My Application?

xiaoxiao2021-03-06  41

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 = loadingLibrary (_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 = loadingLibrary (_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);

转载请注明原文地址:https://www.9cbs.com/read-69675.html

New Post(0)