如何设置热键(“win + Key”)组合来呼叫应用程序? [英] How can I set a hot key ("win+Key") combination to call an application?

查看:107
本文介绍了如何设置热键(“win + Key”)组合来呼叫应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在操作系统级别设置一个热键,一旦设置将会调用任何我告诉它的呼叫。
热键集必须从我的偏好选项中完成,但应用程序不能打开,以便热键工作。

解决方案

这样做你想要的。



首先,你需要一个在后台运行的程序,并且响应击键。像这样:

 程序Project1; 

使用
Windows,消息,ShellAPI;

var
W:HWND;
M:味精

const
WM_SETHOTKEY = WM_APP + 1;
WM_UNSETHOTKEY = WM_APP + 2;
AppName ='Rejbrand Hot Key Listener';

const
文件名:string =('notepad.exe','pbrush.exe')的数组[0..1];

begin

如果FindWindow('STATIC',PChar(AppName))<> 0然后
退出;


W:= CreateWindow('STATIC',PChar(AppName),0,0,0,100,100,HWND_MESSAGE,0,HInstance,nil);

而GetMessage(M,W,0,0)do
case M.message of
WM_HOTKEY:
ShellExecute(0,nil,PChar(FileNames [M .wParam]),nil,nil,SW_SHOWNORMAL);
WM_SETHOTKEY:
RegisterHotKey(W,M.wParam,M.lParam shr 16,M.lParam和$ FFFF);
WM_UNSETHOTKEY:
UnregisterHotKey(W,M.wParam);
结束

结束。

(要创建此程序,请选择New / VCL Forms Application,然后从主窗体中删除主窗体项目,然后选择项目/查看源,并删除 Application.Initialize 废话,程序应该如上所述。)



上述程序监听消息 WM_SETHOTKEY ,注册新的Windows热键, WM_UNSETHOTKEY 删除以前注册的热键,以及由最终用户激活注册热键时由Windows发送的 WM_HOTKEY 。前两条消息由我在本应用程序中定义。



要注册热键,请发送消息 WM_SETHOTKEY 到窗口 W 。消息的 wParam 应该是要启动的程序的索引(在 FileNames 数组中)。 lParam 应该是$ MMMMKKKK,其中$ MMMM是修饰符( Ctrl Alt Shift )和$ KKKK热键的虚拟键代码。要删除热键,请发送 WM_UNSETHOTKEY 消息,程序索引为 wParam W



使用样本



任何你可以做(​​假设Project1.exe在后台运行)

  const 
WM_SETHOTKEY = WM_APP + 1;
WM_UNSETHOTKEY = WM_APP + 2;

const
MODIFIER_ALT = MOD_ALT shl 16;
MODIFIER_CTRL = MOD_CONTROL shl 16;
MODIFIER_SHIFT = MOD_SHIFT shl 16;

程序TForm1.RegisterHotkeys;
var
w:HWND;
begin
w:= FindWindow('STATIC','Rejbrand Hot Key Listener');
PostMessage(w,WM_UNSETHOTKEY,0,MODIFIER_CTRL + MODIFIER_ALT + ord('N'));
PostMessage(w,WM_SETHOTKEY,1,MODIFIER_CTRL + MODIFIER_ALT + ord('P'));
结束

现在,即使关闭这个新程序,notepad.exe和pbrush.exe将从< kbd> Ctrl + Alt + N Ctrl + Alt + < kbd>。



更多讨论



请注意,编译时,Project1.exe只有20 kB小!对于Delphi中的应用程序,这是



要注销以前注册的热键,请执行

  PostMessage(w,WM_UNSETHOTKEY,N,0); 

其中N,在本例中,对于记事本为= 0,pbrush为1。 >

要退出project1.exe,请执行

  PostMessage(w,WM_QUIT,0 ,0); 

但是,当然,如果退出project1.exe,所有热键都被Windows注销。 p>

您可能想要执行

  procedure TForm1.RegisterHotkeys; 
var
w:HWND;
begin
w:= FindWindow('STATIC','Rejbrand Hot Key Listener');
如果w = 0,那么
MessageBox('Error:Rejbrand Hot Key Listener not running!');
PostMessage(w,WM_UNSETHOTKEY,0,MODIFIER_CTRL + MODIFIER_ALT + ord('N'));
PostMessage(w,WM_SETHOTKEY,1,MODIFIER_CTRL + MODIFIER_ALT + ord('P'));
结束

甚至启动project1.exe如果找不到窗口。


I need to set a hot key at the operating system level, that once set will call whatever I tell it to call. The hot key set must be done from inside my preference option, but the application must not have to be open so that the hot key works.

解决方案

This does what you want.

First, you need a program that runs in the background and listens to, and responds to, keystrokes. Like this:

program Project1;

uses
  Windows, Messages, ShellAPI;

var
  W: HWND;
  M: MSG;

const
  WM_SETHOTKEY = WM_APP + 1;
  WM_UNSETHOTKEY = WM_APP + 2;
  AppName = 'Rejbrand Hot Key Listener';

const
  FileNames: array[0..1] of string = ('notepad.exe', 'pbrush.exe');

begin

  if FindWindow('STATIC', PChar(AppName)) <> 0 then
    Exit;


  W := CreateWindow('STATIC', PChar(AppName), 0, 0, 0, 100, 100, HWND_MESSAGE, 0, HInstance, nil);

  while GetMessage(M, W, 0, 0) do
    case M.message of
      WM_HOTKEY:
        ShellExecute(0, nil, PChar(FileNames[M.wParam]), nil, nil, SW_SHOWNORMAL);
      WM_SETHOTKEY:
        RegisterHotKey(W, M.wParam, M.lParam shr 16, M.lParam and $FFFF);
      WM_UNSETHOTKEY:
        UnregisterHotKey(W, M.wParam);
    end;

end.

(To create this program, select New/VCL Forms Application, and then remove the main form from the project. Then select Project/View Source and remove the Application.Initialize nonsense. The program should look like the above.)

The above program listens to the messages WM_SETHOTKEY that registers a new Windows hotkey, WM_UNSETHOTKEY that removes a previously registered hotkey, and WM_HOTKEY that is sent by Windows when a registered hotkey is activated by the end-user. The first two messages are defined by me, in this application.

To register a hotkey, send the message WM_SETHOTKEY to the window W. The wParam of the message should be the index (in the FileNames array) of the program to start. The lParam should be of the form $MMMMKKKK where $MMMM are the modifiers (Ctrl, Alt, Shift) and $KKKK the virtual-key code of the hotkey. To remove a hotkey, send a WM_UNSETHOTKEY message with the program index as wParam to W.

Sample usage

From any application, you can do (assuming that Project1.exe is running in the background)

const
  WM_SETHOTKEY = WM_APP + 1;
  WM_UNSETHOTKEY = WM_APP + 2;

const
  MODIFIER_ALT = MOD_ALT shl 16;
  MODIFIER_CTRL = MOD_CONTROL shl 16;
  MODIFIER_SHIFT = MOD_SHIFT shl 16;

procedure TForm1.RegisterHotkeys;
var
  w: HWND;
begin
  w := FindWindow('STATIC', 'Rejbrand Hot Key Listener');
  PostMessage(w, WM_UNSETHOTKEY, 0, MODIFIER_CTRL + MODIFIER_ALT + ord('N'));
  PostMessage(w, WM_SETHOTKEY, 1, MODIFIER_CTRL + MODIFIER_ALT + ord('P'));
end;

Now, even if you close this new program, notepad.exe and pbrush.exe will start on Ctrl + Alt + N and Ctrl + Alt + P, respectively.

Some more discussion

Notice that, when compiled, Project1.exe is only 20 kB small! This is tiny for an application made in Delphi!

To unregister a previously registered hotkey, do

PostMessage(w, WM_UNSETHOTKEY, N, 0);

where N, in this example, is = 0 for notepad and = 1 for pbrush.

To quit project1.exe, do

PostMessage(w, WM_QUIT, 0, 0);

But, of course, if you quit project1.exe, all hotkeys are unregistered by Windows.

You might want to do

procedure TForm1.RegisterHotkeys;
var
  w: HWND;
begin
  w := FindWindow('STATIC', 'Rejbrand Hot Key Listener');
  if w = 0 then
    MessageBox('Error: Rejbrand Hot Key Listener not running!');
  PostMessage(w, WM_UNSETHOTKEY, 0, MODIFIER_CTRL + MODIFIER_ALT + ord('N'));
  PostMessage(w, WM_SETHOTKEY, 1, MODIFIER_CTRL + MODIFIER_ALT + ord('P'));
end;

or even start project1.exe if you cannot find the window.

这篇关于如何设置热键(“win + Key”)组合来呼叫应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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