说三道四技术文摘-感悟人生的经典句子
说三道四 > 文档快照

定义全局热键-VB资料

HTML文档下载 WORD文档下载 PDF文档下载
定义全局热键-VB资料

按下某组键(HotKey)便执行某程式
在Dos的年代,我们常会以拦截中断向量的方式,做到按下某个hotkey而自动执行某个程式,在Window呢,也可以,不过它是用RegisterHotkey API来完成。

使用RegisterHotkey的概念是,它会定义一组按键的组合,当使用者不管在哪个程式之中,按下Window有注册的HotKey时,OS会传送WM_HOTKEY 的讯息给待接收该讯息的Window,而该Window收到WM_HOTKEY时,便可知道有本身Thread所定义的HotKey被按下,於是可以从wParam, lParam来得知是哪一组HotKey被按下。

RegisterHotKey(
ByVal hwnd As Long , //接收Hotkey的Window
ByVal idHotKey as Long, // identifier of hot key,range 0x0000 through 0xBFFF
ByVal Modifiers As Long, // 定义alt shift control等的组合
ByVal uVirtKey As Long // virtual-key code
)

WM_HOTKEY 叁数的定义
idHotKey = wParam; // identifier of hot key
Modifiers = (UINT) LOWORD(lParam); // key-modifier flags
uVirtKey = (UINT) HIWORD(lParam); // virtual-key code

所以了,除了设定RegisterHotkey外,另要使用SubClassing的技巧才会得知HotKey被按下;最後,程式结束前要使用UnRegisterHotkey将HotKey的定义取消掉。

以下程式功能是:不管在哪个程式中,只要按下ALT-SHIFT-G 便执行NotePad。
'以下在.Bas
Option Explicit

Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long

Public Const WM_HOTKEY = &H312
Public Const MOD_ALT = &H1
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4
Public Const GWL_WNDPROC = (-4)

Public preWinProc As Long
Public Modifiers As Long, uVirtKey As Long, idHotKey As Long

Private Type taLong
ll As Long
End Type

Private Type t2Int
lWord As Integer
hword As Integer
End Type

Public Function wndproc(ByVal hwnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = WM_HOTKEY Then
If wParam = idHotKey Then
Dim lp As taLong, i2 As t2Int
lp.ll = lParam
LSet i2 = lp
If (i2.lWord = Modifiers) And i2.hword = uVirtKey Then
Debug.Print "HotKey Shift-Alt-G Pressed "
Shell "notepad", vbNormalFocus
End If
End If
End If
'将之送往原来的Window Procedure
wndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)
End Function

'以下在 Form
Sub Form_Load()
Dim ret As Long
preWinProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf wndproc)
idHotKey = 1 'in the range &h0000 through &hBFFF
Modifiers = MOD_ALT + MOD_SHIFT
uVirtKey = vbKeyG
ret = RegisterHotKey(Me.hwnd, idHotKey, Modifiers, uVirtKey)
End Sub

Private Sub Form_Unload(Cancel As Integer)
Dim ret As Long
'取消Message的截取,而使之又只送往原来的Window Procedure
ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, preWinProc)
Call UnregisterHotKey(Me.hwnd, uVirtKey)
End Sub



微信沟通接口上线,开启移动应用与公众号的连接 【特惠最后1天】参加深圳微信开发者大会赠书及全套讲师讲义(附大会最新日程) 编码运行两不误!KZPlayground,Objective-C中的Playgrounds 专访PopCap:AR是数字与物质世界的调和剂 【多图】Github编程语言分布统计:JavaScript称霸 picture控件的问题 大家来讨论,在数据库MS-SQLServer中,要达到什么样的程度才算高手! 大家来讨论,在数据库MS-SQLServer中,要达到什么样的程度才算高手! mysql的备份策略! !!!!裸女跪求!!!!几个小问题,,请帮忙,,,,,55555555(UP的有分) 在页面上使用时间控件不可见? 100分求可以MASM编译单片机程序的仿真器 讨论关于三层结构 祝tacoell(声雨山)明天生日快乐:) 高透明PP的杯子装热水有毒吗在超市里买了一包饼干, 食品级PP塑胶有毒吗 问下PP点点通有毒吗? pp材质的碗有毒吗 有色的PP材料有没有毒?我要买一个便当盒,说是PP pp材质有毒吗 聚丙烯(PP)树脂的老化?聚丙烯树脂容器在户外放置 pp是几号塑料 pp塑料是什么 日本政府拟制定相关措施援助海外核爆受德银:离岸人民币外汇期权市场迅速发展日本防相:监视中国海军演习符合国际法北京一周内两发空气重污染蓝色预警黑龙江青年支教山西10余年 月俸仅6主席的一席话,多忙也看看死了谁说女人四十豆腐渣!荐号 | 维系一生,我所愿意俞敏洪:什么是男人的气质?神陨血性异界法神盘龙后传秘宝迷踪代县旅游环江旅游吉木萨尔旅游成县旅游上杭旅游
备案号:鲁ICP备13029499号-2 说三道四 www.s3d4.cn 说三道四技术文摘