如何将键盘功能键进行转换?十分着急!!!(100分)

  • 主题发起人 主题发起人 plf22
  • 开始时间 开始时间
P

plf22

Unregistered / Unconfirmed
GUEST, unregistred user!
现在遇到一个棘手的问题,想要把键盘上WIN键的功能通过程序转成ALT键的功能,也就是按下
WIN键后的效果是ALT键的效果,这个问题十分急,希望各位帮帮忙,最好今天能搞定,公司这
两天要发货了
 
由于Win键很特殊,不能用窗体的OnKeyDown事件进行拦截,要设键盘HOOK才行。(我没有做过)
你查查看论坛中SetWindowsHookEx关键字,应该能找到很多以前的回答。
 
我的鼠标和键盘Hook.
你只需要在 KeyboardHookCallBack 判断keycode是不是WIN键,如果是就在
Result := CallNextHookEx(HookHandle, Code, KeyCode, Info);
这一行把keycode改成你要的键的键值.(理论上是,我没试过,我的hook程序是正确的)

library Hookdll2;

uses WinTypes, WinProcs, Messages;

type
PSharedData = ^TSharedData;
TSharedData = record
HookCount: integer;
MonitorWnd: HWND;

MouseHookHandle: HHook;
WM_MONITORMOUSEMOVE: UINT;

KeyBoardHookHandle: HHook;
WM_MONITORKEYBOARD: UINT;
end;

var
hMapObject: THandle;
SharedData: PSharedData;


const
MESSAGE_MONITOR_MOUSE_MOVE = 'CEMIHookDLLMonitorMouseMoveMessage';
MESSAGE_MONITOR_KEYBOARD = 'CEMIHookDLLMonitorKeyboardMessage';


function GetMonitorMouseMoveMsg: UINT; export;
begin
if SharedData = NIL then
Result := 0
else
Result := SharedData^.WM_MONITORMOUSEMOVE;
end;

function GetMonitorKeyboardMsg: UINT; export;
begin
if SharedData = NIL then
Result := 0
else
Result := SharedData^.WM_MONITORKEYBOARD;
end;

function MouseHookCallBack(Code: integer; Msg: WPARAM; MouseHook: LPARAM): LRESULT; stdcall;
var
HookHandle: HHOOK;
MonitorWnd: HWND;
WM_MONITORMOUSEMOVE: UINT;
MouseHookStruct: PMouseHookStruct absolute MouseHook;
begin
if SharedData <> NIL then
begin
MonitorWnd := SharedData^.MonitorWnd;
HookHandle := SharedData^.MouseHookHandle;
WM_MONITORMOUSEMOVE := SharedData^.WM_MONITORMOUSEMOVE;
end
else
begin
WM_MONITORMOUSEMOVE := 0;
MonitorWnd := 0;
HookHandle := 0;
end;

if (Code >= 0) and (MonitorWnd <> 0) then
begin
if (Msg = WM_MOUSEMOVE) and (MouseHookStruct <> NIL) then
PostMessage(MonitorWnd, WM_MONITORMOUSEMOVE, MouseHookStruct^.pt.x,
MouseHookStruct^.pt.y);
Result := CallNextHookEx(HookHandle, Code, Msg, MouseHook);
end
else
Result := CallNextHookEx(HookHandle, Code, Msg, MouseHook);
end;

function KeyboardHookCallBack(Code: integer; KeyCode: WPARAM; Info: LPARAM): LRESULT; stdcall;
var
HookHandle: HHOOK;
MonitorWnd: HWND;
WM_MONITORKEYBOARD: UINT;
begin
if SharedData <> NIL then
begin
MonitorWnd := SharedData^.MonitorWnd;
HookHandle := SharedData^.KeyboardHookHandle;
WM_MONITORKEYBOARD := SharedData^.WM_MONITORKEYBOARD;
end
else
begin
WM_MONITORKEYBOARD := 0;
MonitorWnd := 0;
HookHandle := 0;
end;

if (Code >= 0) and (MonitorWnd <> 0) then
begin
PostMessage(MonitorWnd, WM_MONITORKEYBOARD, KeyCode, Info);
Result := CallNextHookEx(HookHandle, Code, KeyCode, Info);
end
else
Result := CallNextHookEx(HookHandle, Code, KeyCode, Info);
end;

function InstallHook(SystemHook: boolean; TaskHandle: THandle;
AMonitorWnd: HWND) : boolean; export;
function GetModuleHandleFromInstance: THandle;
var
s: array[0..512] of char;
begin
GetModuleFileName(hInstance, s, sizeof(s)-1);
Result := GetModuleHandle(s);
end;

begin
Result := FALSE;
if SharedData = NIL then
exit
else
with SharedData^ do
begin
Result := TRUE;
if HookCount = 0 then
begin
MonitorWnd := AMonitorWnd;
if SystemHook then
begin
MouseHookHandle := SetWindowsHookEx(WH_MOUSE, MouseHookCallBack, HInstance, 0);
KeyboardHookHandle := SetWindowsHookEx(WH_KEYBOARD, KeyboardHookCallBack, HInstance, 0);
end
else
begin
MouseHookHandle := SetWindowsHookEx(WH_MOUSE, MouseHookCallBack,
GetModuleHandleFromInstance, TaskHandle);
KeyboardHookHandle := SetWindowsHookEx(WH_KEYBOARD, KeyboardHookCallBack,
GetModuleHandleFromInstance, TaskHandle);
end;
if (MouseHookHandle <> 0) and (KeyboardHookHandle <> 0) then
inc(HookCount)
else
Result := FALSE;
end
else
inc(HookCount);
end;
end;

function RemoveHook: boolean; export;
var
MHook, KHook: boolean;
begin
Result := FALSE;
if SharedData = NIL then
exit
else
with SharedData^ do
begin
if HookCount < 1 then exit;
Result := TRUE;
dec(HookCount);
if HookCount = 0 then
begin
MHook := UnhookWindowsHookEx(MouseHookHandle);
KHook := UnhookWindowsHookEx(KeyboardHookHandle);
result := MHook and KHook;
end;
end;
end;

function IsHookSet: boolean; export;
begin
if SharedData = NIL then
Result := FALSE
else
with SharedData^ do
Result := (HookCount > 0) and (MouseHookHandle <> 0) and (KeyboardHookHandle <> 0);
end;

procedure AllocSharedData;
var
Init: boolean;
begin
if hMapObject = 0 then
begin
hMapObject := CreateFileMapping(
THandle($FFFFFFFF), NIL, PAGE_READWRITE, 0, SizeOf(TSharedData),
'DFSHookDLLSharedDataBlock');
Init := GetLastError <> ERROR_ALREADY_EXISTS;
if hMapObject = 0 then exit;
SharedData := MapViewOfFile( hMapObject, FILE_MAP_WRITE, 0, 0, 0);
if SharedData = NIL then exit;
if Init then
FillChar(SharedData^, SizeOf(TSharedData), 0);
SharedData^.WM_MONITORMOUSEMOVE := RegisterWindowMessage(MESSAGE_MONITOR_MOUSE_MOVE);
SharedData^.WM_MONITORKEYBOARD := RegisterWindowMessage(MESSAGE_MONITOR_KEYBOARD);
end;
end;

procedure FreeSharedData;
begin
if hMapObject <> 0 then
try
try
UnMapViewOfFile(SharedData);
CloseHandle(hMapObject);
except
end;
finally
SharedData := NIL;
hMapObject := 0;
end;
end;

procedure EntryPointProc(Reason: Integer);
begin
case Reason of
DLL_PROCESS_DETACH: FreeSharedData;
DLL_PROCESS_ATTACH: AllocSharedData;
DLL_THREAD_ATTACH: {} ;
DLL_THREAD_DETACH: {} ;
end;
end;

exports
GetMonitorMouseMoveMsg,
GetMonitorKeyboardMsg,
InstallHook,
RemoveHook,
IsHookSet,
KeyboardHookCallBack,
MouseHookCallBack;

begin
SharedData := NIL;
hMapObject := 0;
DllProc := @EntryPointProc;
EntryPointProc(DLL_PROCESS_ATTACH);
end.
 
好麻烦,我做的是个工控机的程序,结果键盘被人设计成有个WIN键而没有ALT键,我要这个
ALT键是为了激活菜单,没有了这个ALT键,还有没有别的办法可以激活菜单的,工控机的键
盘上只有方向键,退格键,回车键和没用的WIN键,各位再帮忙想想办法,先谢了
 
你只需要编一个程序把那个DLL加载,然后启动系统就运行那个程序,这样不可以吗?
 
MilkRoad:
我现在的这个程序是用VB写的,如果用DELPHI就可以,不过VB不太精通,所以我主要想知道
一些解决的思路
 
我想应该是有两个方法,
1、可以用Hook的办法,就是钩子程序,外挂一个DLL实现该功能
2、你还可以试一下注册系统热键的办法(registerhotkey)
 
你是什么系统 WIN98/NT/2000?
在 NT/Win 2000 环境下,需要用到低级键盘钩子,才能捕获到这类系统按键。
 
你用delphi写一个程序来加载那个dll就可以了,并且那个程序跟你VB的程序是不相关的,
键盘hook可以让你截获所有的键盘消息,然后根据你的需要来处理,你的解决方法是在
dll中收到WIN键的消息后把它改成ATL键然后再发回消息队列中就可以了.这样整个系统
的Win键都会变成ATL键.
 
后退
顶部