Delphi XE2 - 10:全球热键

时间:2016-01-28 14:56:33

标签: delphi

j有一个主程序,可以使用条形码阅读器和没有键盘的键盘。 当在此程序中按任何数字键或返回键(或通过条形码阅读器模拟)时,如果插入代码存储在数据库中,则需要检入我的应用程序(在try栏中运行idden)。 为此,我尝试了全球热键。 这里是示例项目

Type
TForm1 = class(TForm)
  procedure FormCreate(Sender: TObject);
  procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
  id0, id1, id2, id3  : Integer;
  id4, id5, id6, id7  : Integer;
  id8,id9,IdEnter     : Integer;
  Code                : string;
  procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY;
public
{ Public declarations }
end;

Var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
const
  K_0   = $30;
  K_1   = $31;
  K_2   = $32;
  K_3   = $33;
  K_4   = $34;
  K_5   = $35;
  K_6   = $36;
  K_7   = $37;
  K_8   = $38;
  K_9   = $39;

begin
  // Register from 0 to 9 and Enter
  id0 := GlobalAddAtom('K0');
  RegisterHotKey(Handle, id0, 0, K_0);
  id1 := GlobalAddAtom('K1');
  RegisterHotKey(Handle, id1, 0, K_1);
  id2 := GlobalAddAtom('K2');
  RegisterHotKey(Handle, id2, 0, K_2);
  id3 := GlobalAddAtom('Key3');
  RegisterHotKey(Handle, id3, 0, K_3);
  id4 := GlobalAddAtom('Key4');
  RegisterHotKey(Handle, id4, 0, K_4);
  id5 := GlobalAddAtom('Key5');
  RegisterHotKey(Handle, id5, 0, K_5);
  id6 := GlobalAddAtom('Key6');
  RegisterHotKey(Handle, id6, 0, K_6);
  id7 := GlobalAddAtom('Key7');
  RegisterHotKey(Handle, id7, 0, K_7);
  id8 := GlobalAddAtom('Key8');
  RegisterHotKey(Handle, id8, 0, K_8);
  id9 := GlobalAddAtom('Key9');
  RegisterHotKey(Handle, id9, 0, K_9);
  IdEnter := GlobalAddAtom('KeyEnter');
  RegisterHotKey(Handle, idEnter, 0, VK_RETURN);
  Code:='';
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  UnRegisterHotKey(Handle, id0);
  GlobalDeleteAtom(id0);
  UnRegisterHotKey(Handle, id1);
  GlobalDeleteAtom(id1);
  UnRegisterHotKey(Handle, id2);
  GlobalDeleteAtom(id2);
  UnRegisterHotKey(Handle, id3);
  GlobalDeleteAtom(id3);
  UnRegisterHotKey(Handle, id4);
  GlobalDeleteAtom(id4);
  UnRegisterHotKey(Handle, id5);
  GlobalDeleteAtom(id5);
  UnRegisterHotKey(Handle, id6);
  GlobalDeleteAtom(id6);
  UnRegisterHotKey(Handle, id7);
  GlobalDeleteAtom(id7);
  UnRegisterHotKey(Handle, id8);
  GlobalDeleteAtom(id8);
  UnRegisterHotKey(Handle, id9);
  GlobalDeleteAtom(id9);
  UnRegisterHotKey(Handle, IdEnter);
  GlobalDeleteAtom(IdEnter);
end;

procedure TForm1.WMHotKey(var Msg: TWMHotKey);
begin
  if Msg.HotKey = id0 then Code:= Code +'0';
  if Msg.HotKey = id1 then Code:= Code +'1';
  if Msg.HotKey = id2 then Code:= Code +'2';
  if Msg.HotKey = id3 then Code:= Code +'3';
  if Msg.HotKey = id4 then Code:= Code +'4';
  if Msg.HotKey = id5 then Code:= Code +'5';
  if Msg.HotKey = id6 then Code:= Code +'6';
  if Msg.HotKey = id7 then Code:= Code +'7';
  if Msg.HotKey = id8 then Code:= Code +'8';
  if Msg.HotKey = id9 then Code:= Code +'9';
  if Msg.HotKey = IdEnter then
  begin
    ShowMessage('ENTER pressed ! ' + #13 + Code); 
    Code:='';
  end;
end;

end.

这是有效的,如果按下任何数字键,字符串将增加,如果按下返回,则消息显示但是在任何其他Windows应用程序(记事本,即主程序中),此键似乎被禁用(可能是因为热键)。

请有人知道如何在第二个程序(我的程序)中输入在主程序(而不是我的程序)中键入的代码吗?

感谢所有感谢您的回复

方面

丹尼尔

1 个答案:

答案 0 :(得分:1)

当你打电话给RegisterHotKey时,你告诉系统你的处理它处理注册密钥的所有按下。因此,键只在您的过程中起作用并被其他过程忽略,这是很自然的。这是设计的。

您需要做的是使用键盘钩。最简单的一个是低级键盘钩。这很简单,因为它不需要注射。使用SetWindowsHookEx传递WH_KEYBOARD_LL