首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >德尔福XE2 - 10:全球热键

德尔福XE2 - 10:全球热键
EN

Stack Overflow用户
提问于 2016-01-28 14:56:32
回答 1查看 954关注 0票数 1

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

代码语言:javascript
复制
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应用程序(记事本,即和主程序中)中,这些键似乎被禁用(可能是热键)。

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

谢谢你的回复

关注

丹妮尔

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-28 15:06:45

当您调用RegisterHotKey时,您告诉系统您要处理它来处理所有已注册密钥的按下。因此,键只在您的过程中起作用,而被其他进程忽略,这是很自然的。那是故意的。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35064794

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档