【问题标题】:Delphi XE2 - 10: Global hotkeyDelphi XE2 - 10:全局热键
【发布时间】:2016-01-28 14:56:32
【问题描述】:

j 有一个主程序,可与条码阅读器和无键盘的键盘配合使用。 当在此程序中按下任何数字键或返回键(或通过条形码阅读器模拟)时,如果插入代码存储在数据库中,则需要检查我的应用程序(在尝试栏中运行 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 应用程序(记事本,即在主程序中)中,此键似乎被禁用(可能是因为热键)。

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

感谢您的所有感谢回复

注意

丹尼尔

【问题讨论】:

    标签: delphi


    【解决方案1】:

    当您致电RegisterHotKey 时,您告诉系统您的进程将处理所有注册键的按下。所以很自然,key 只在你的进程中起作用,而被其他进程忽略。这是设计使然。

    您需要做的是使用键盘挂钩。最简单的写法是低级键盘钩子。这很简单,因为它不需要注入。使用SetWindowsHookEx 传递WH_KEYBOARD_LL

    【讨论】:

    • 感谢您的快速回复;使用 hook j 解决了这个问题。
    猜你喜欢
    • 1970-01-01
    • 2016-02-28
    • 2010-12-12
    • 2019-12-07
    • 2016-02-22
    • 2018-05-01
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多