在主窗体上放一个ApplicationEvents控件,在它的OnMessage事件中: procedure TMainForm.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean); begin if Msg.message = WM_KEYDOWN then case Msg.wParam of VK_ADD: ShowMessage('+'); VK_SUBTRACT: ShowMessage('-'); else // ShowMessage(IntToStr(Msg.wParam)); end; end;
把事件都写在ApplicationEvents中的函数中
使用函数Screen.ActiveControl.Perform,下面例子是按Delete键,做Delete的工作。 var State : TKeyboardState; begin GetKeyboardState(State); // press 'DEL' key if ((State[vk_Delete] and 128) <> 0) then begin if (Screen.ActiveControl.Owner is TForm) and begin DeleteSomething; //删除工作 end else Screen.ActiveControl.Perform(WM_KEYDOWN, VK_DELETE, 0) end else begin DeleteSomething; //删除工作 end; end end;加分吧!
在主窗体一个TActionList,在TActionList加一个Delete 的Action,并写上以下代码在OnExcute事件上,当按Delete键时,系统就会做做Delete的工作。 var State : TKeyboardState; begin GetKeyboardState(State); // press 'DEL' key if ((State[vk_Delete] and 128) <> 0) then begin if (Screen.ActiveControl.Owner is TForm) and begin DeleteSomething; //删除工作 end else Screen.ActiveControl.Perform(WM_KEYDOWN, VK_DELETE, 0) end else begin DeleteSomething; //删除工作 end; end end;
procedure TMainForm.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
begin
if Msg.message = WM_KEYDOWN then
case Msg.wParam of
VK_ADD: ShowMessage('+');
VK_SUBTRACT: ShowMessage('-');
else
// ShowMessage(IntToStr(Msg.wParam));
end;
end;
var
State : TKeyboardState;
begin
GetKeyboardState(State);
// press 'DEL' key
if ((State[vk_Delete] and 128) <> 0) then
begin
if (Screen.ActiveControl.Owner is TForm) and
begin
DeleteSomething; //删除工作
end
else
Screen.ActiveControl.Perform(WM_KEYDOWN, VK_DELETE, 0)
end
else
begin
DeleteSomething; //删除工作
end;
end
end;加分吧!
var
State : TKeyboardState;
begin
GetKeyboardState(State);
// press 'DEL' key
if ((State[vk_Delete] and 128) <> 0) then
begin
if (Screen.ActiveControl.Owner is TForm) and
begin
DeleteSomething; //删除工作
end
else
Screen.ActiveControl.Perform(WM_KEYDOWN, VK_DELETE, 0)
end
else
begin
DeleteSomething; //删除工作
end;
end
end;