Delphi XE3 WM_HOTKEY如何判断何时发布热键? [英] Delphi XE3 WM_HOTKEY How to tell when HotKey is Released ?

查看:64
本文介绍了Delphi XE3 WM_HOTKEY如何判断何时发布热键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在通信程序中编写一个咳嗽按钮,但该程序并不总是具有焦点。我有使麦克风静音和取消静音(MMDevApi)的代码,它可以完美工作。我设置了一个全局热键,它非常适合设置静音。现在的问题。我如何知道何时释放热键?我尝试了代码中所示的计时器,但是它的行为很奇怪。我按住我的热键,麦克风立即静音,然后在计时器间隔后将其取消静音一段时间(似乎是计时器间隔的一半),然后静音并保持静音。如果没有计时器,它会静音并保持静音状态。我真的不希望(或认为这样很好),不必再按第二个键才能使麦克风静音。

I am trying to program a "cough button" in a communications program that does not always have focus. I have the code working to mute and unmute the mic (MMDevApi) and it works perfect. I set up a global hot key and this works perfect to set the mute. Now the problem. How do I tell when the Hotkey is released ? I tried a timer as shown in the code but it has an odd behavior. I Hold down my hotkey and the mic mutes immediately then after the interval of the timer it unmutes for a what appears to be half the interval of the timer and then it mutes and stays muted. Without the timer it mutes and stays muted perfectly. I really don't want ( or think it would be any good ) to have to hit a second key to unmute the mic.

//here is my register hot key code ! 
CoughKeyWnd := AllocateHwnd(CoughKeyWndProc);
CoughKey := GlobalAddAtom('CoughKey');
  if CoughKey <> 0 then
    RegisterHotKey(CoughKeyWnd, CoughKey, MOD_CONTROL, VK_OEM_3);

//the procedure
procedure TForm1.CoughKeyWndProc(var Message: TMessage);
begin
 if Message.Msg = WM_HOTKEY then
  begin // to prevent recalling mute 
    if CoughOn = FALSE then
 begin
   CoughOn := True;
   CoughOff.SetMute(1,@GUID_NULL);
 end;
  Timer1.Enabled := FALSE;
  Timer1.Enabled := True;
end
   else
    begin
      Message.Result := DefWindowProc(CoughKeyWnd, Message.Msg, Message.WParam, Message.LParam);
    end;

//and finally the ontimer ! 
procedure TForm1.JvTimer1Timer(Sender: TObject);
begin
  CoughOff.SetMute(0,@GUID_NULL);
  Timer1.Enabled := False;
  CoughOn := False;

end;


推荐答案

如果您使用计时器,就会看到这种行为在检索第二个 WM_HOTKEY 之前到期,但在检索连续消息时不会到期。第一和第二消息之间的时间范围大于连续消息之间的时间范围。这是因为键盘延迟大于键盘重复间隔(典型值为〜250ms)。

You'll see that kind of behavior if your timer expires before the second WM_HOTKEY is retrieved but does not expire retrieving consecutive messages. The time frame between the first and second message is greater than the time frame between consecutive messages. This is because keyboard delay is greater (~250ms typical) than keyboard repeat interval.

要使您的方法有效,请增加计时器间隔,例如两次键盘延迟。您可以使用 SystemParametersInfo 来获取键盘延迟的近似值。或使用最小时间框架使麦克风保持静音状态,然后再开始观察重复的热键消息以重新启用计时器。但是,此方法仍然有些不可靠,无论出于何种原因,热键消息都可能会延迟。最好在计时器处理程序中使用 GetKeyState 来测试按键是否仍然按下。

To make your approach work, increase your timer interval, something like twice the keyboard delay for instance. You can use SystemParametersInfo to get an approximation for keyboard delay. Or employ a minimum time frame for the microphone to stay in muted state, and only after then start watching for repeated hotkey messages to re-enable your timer. Still, this method will be somewhat unreliable, hotkey messages might be delayed for whatever reason. Better use GetKeyState in your timer handler to test if the keys are still down.

您可以安装键盘钩或者如果您不想使用计时器,则在按下热键时注册原始输入。

You can install a keyboard hook or register for raw input when the hotkey is hit if you don't want to use the timer.

这篇关于Delphi XE3 WM_HOTKEY如何判断何时发布热键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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