确定是否按SHIFT键并在键盘上进行了Hook Proc [英] Determine if SHIFT is pressed insde keyboard Hook Proc

查看:118
本文介绍了确定是否按SHIFT键并在键盘上进行了Hook Proc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的钩子进程中,我如何确定某个使用过的用户是否先按SHIFT键(而不释放它),然后再按一个char键(如A键)?

inside my hook proc, how can i determine whether a used is pressing SHIFT (without releasing it) and then a char key (like A) ?

例如,如果我按Shift + A,我想知道它将是一个大写字母a,而不是像Shift,a那样得到它

for example, if i press Shift+A, i want to know that it will be an uppercase a instead of getting it like Shift, a

因此它将是A,如果用户按下并释放Shift,它将仅捕获Shift.

so it will be A, if a user presses and releases Shift, it will capture Shift only.

安装钩为WH_KEYBOARD(全局)

the instaleld hook is WH_KEYBOARD (Global)

function KeyHookProc(Code: Integer; wVirtKey: WPARAM; lKeyStroke: LPARAM)
  : LRESULT; stdcall;
type
  TTransitionState = (tsPressed, tsReleased);

  PKeystrokeData = ^TKeystrokeData;

  TKeystrokeData = record
    VirtualKey: WPARAM;
    KeyStroke: LPARAM;
    KeyState: TKeyboardState;
  end;
var
  Transition: TTransitionState;
  KeystrokeDataPtr: PKeystrokeData;
begin
  Result := CallNextHookEx(hKeyHook, Code, wVirtKey, lKeyStroke);

  Transition := TTransitionState((lKeyStroke shr 31) and 1);

  if (Code = HC_ACTION) and (Transition = tsPressed) then
  begin
    New(KeystrokeDataPtr);
    try
      KeystrokeDataPtr^.VirtualKey := wVirtKey;
      KeystrokeDataPtr^.KeyStroke := lKeyStroke;
      GetKeyboardState(KeystrokeDataPtr^.KeyState);
      SendMessage(hConsole, WM_NULL, 0, LPARAM(KeystrokeDataPtr));
    finally
      Dispose(KeystrokeDataPtr);
    end;
  end;
end;

推荐答案

以下是我们日常正常使用的用于检测Shift键的代码.我从来没有在一个迷惑的上下文中使用过它,所以我不知道它是否可以在那里工作,或者在那个上下文中是否有其他东西可以阻止它.

Here's the code we use in normal day-to-day use to detect the shift key. I've never used it in a hooked context, so I don't know if it would work there, or if something is different in that context that would prevent it.

function ShiftIsDown : Boolean;
var
  State: TKeyboardState;
begin
  WINDOWS.GetKeyboardState(State);
  Result := ((State[vk_Shift] and 128) <> 0);
end; 

这篇关于确定是否按SHIFT键并在键盘上进行了Hook Proc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆