本来是想做键盘映射的,没能实现,现在只是键盘监听而已,求指导。。。
动态链接库代码:
KeyMapDll.h
1/**//*
2KeyMapDll.h
3
4Copyright (C) 2011, coreBugZJ, all rights reserved.
5
6键盘映射 动态链接库部分
7*/
8
9
10#ifndef __KEYMAPDLL_H_INCLUDED__
11#define __KEYMAPDLL_H_INCLUDED__
12
13
14#ifdef KEYMAP
15#define KEYMAPAPI __declspec(dllexport)
16#else
17#define KEYMAPAPI __declspec(dllimport)
18#endif
19
20
21 /**//* 执行成功,返回 TRUE */
22BOOL KEYMAPAPI WINAPI startKeyMap();
23 /**//* 执行成功,返回 TRUE */
24BOOL KEYMAPAPI WINAPI stopKeyMap();
25
26
27#endif
28
KeyMapDll.cpp
1/**//*
2KeyMapDll.cpp
3
4Copyright (C) 2011, coreBugZJ, all rights reserved.
5
6键盘映射 动态链接库部分
7*/
8
9
10#define KEYMAP
11
12
13#include <Windows.h>
14#include "KeyMapDll.h"
15
16
17#pragma data_seg("KeyMapShare")
18HHOOK hHook = NULL;
19#pragma data_seg()
20
21
22LRESULT CALLBACK KeyProc( INT code, WPARAM wParam, LPARAM lParam ) {
23 if ( (0 > code) || (HC_NOREMOVE == code) ) {
24 return ::CallNextHookEx( hHook, code, wParam, lParam );
25 }
26 /**//*
27 // 监听键盘
28 switch ( wParam ) {
29 case 虚拟键码 :
30 }
31 */
32 return ::CallNextHookEx( hHook, code, wParam, lParam );
33}
34
35BOOL KEYMAPAPI WINAPI startKeyMap() {
36 MEMORY_BASIC_INFORMATION mbi;
37 HMODULE hModule;
38 if ( NULL != hHook ) {
39 return TRUE;
40 }
41 if ( 0 == ::VirtualQuery( KeyProc, &mbi, sizeof(mbi) ) ) {
42 return FALSE;
43 }
44 hModule = (HMODULE)mbi.AllocationBase;
45 hHook = ::SetWindowsHookEx( WH_KEYBOARD, KeyProc, hModule, 0 );
46 return ( NULL != hHook );
47}
48
49BOOL KEYMAPAPI WINAPI stopKeyMap() {
50 BOOL res;
51 if ( NULL == hHook ) {
52 return TRUE;
53 }
54 res = ::UnhookWindowsHookEx( hHook );
55 hHook = NULL;
56 return res;
57}
58
KeyMapDll.def
1LIBRARY KeyMapDll
2EXPORTS
3 startKeyMap
4 stopKeyMap
5SECTIONS
6 KeyMapShare Read Write Shared
7 启动部分:
KeyMap.cpp
1/**//*
2KeyMap.cpp
3
4Copyright (C) 2011, coreBugZJ, all rights reserved.
5
6键盘映射 应用程序部分
7*/
8
9
10#include <Windows.h>
11#include "KeyMapDll.h"
12#include <stdio.h>
13
14
15#pragma comment( lib, "KeyMapDll.lib" )
16
17
18int main() {
19 startKeyMap();
20 scanf( "%*c" );
21 stopKeyMap();
22 return 0;
23}
24