VB6 注册系统热键

不知道有没有更好的方法?

我用 RegisterHotKey 注册了系统热键

看了一些代码,觉得感觉不是很好,用的是死循环来监视热键,例如

这个就是用死循环,加了个DoEvents,防止CPU100%,或者是用了Timer控件的

 

 1 Private   Const  MOD_ALT  =   & H1
 2 Private   Const  MOD_CONTROL  =   & H2
 3 Private   Const  MOD_SHIFT  =   & H4
 4 Private   Const  PM_REMOVE  =   & H1
 5 Private   Const  WM_HOTKEY  =   & H312
 6 Private  Type POINTAPI
 7     x  As   Long
 8     y  As   Long
 9 End  Type
10 Private  Type Msg
11     hWnd  As   Long
12     Message  As   Long
13     wParam  As   Long
14     lParam  As   Long
15      time   As   Long
16     pt  As  POINTAPI
17 End  Type
18 Private  Declare  Function  RegisterHotKey Lib  " user32 "  (ByVal hWnd  As   Long , ByVal id  As   Long , ByVal fsModifiers  As   Long , ByVal vk  As   Long As   Long
19 Private  Declare  Function  UnregisterHotKey Lib  " user32 "  (ByVal hWnd  As   Long , ByVal id  As   Long As   Long
20 Private  Declare  Function  PeekMessage Lib  " user32 "  Alias  " PeekMessageA "  (lpMsg  As  Msg, ByVal hWnd  As   Long , ByVal wMsgFilterMin  As   Long , ByVal wMsgFilterMax  As   Long , ByVal wRemoveMsg  As   Long As   Long
21 Private  Declare  Function  WaitMessage Lib  " user32 "  ()  As   Long
22 Private  bCancel  As   Boolean
23 Private   Sub  ProcessMessages()
24      Dim  Message  As  Msg
25      ' loop until bCancel is set to True
26      Do   While   Not  bCancel
27          ' wait for a message
28         WaitMessage
29          ' check if it's a HOTKEY-message
30          If  PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE)  Then
31              ' minimize the form
32             WindowState  =  vbMinimized
33          End   If
34          ' let the operating system process other events
35         DoEvents
36      Loop
37 End Sub
38 Private   Sub  Form_Load()
39      Dim  ret  As   Long
40     bCancel  =   False
41      ' register the Ctrl-F hotkey
42     ret  =  RegisterHotKey(Me.hWnd,  & HBFFF & , MOD_CONTROL, vbKeyF)
43      ' show some information
44     Me.AutoRedraw  =   True
45     Me.Print  " Press CTRL-F to minimize this form "
46      ' show the form and
47     Show
48      ' process the Hotkey messages
49     ProcessMessages
50 End Sub
51 Private   Sub  Form_Unload(Cancel  As   Integer )
52     bCancel  =   True
53      ' unregister hotkey
54      Call  UnregisterHotKey(Me.hWnd,  & HBFFF & )
55 End Sub
56
57


'//第2个示例
'演示怎样设置一个窗口在桌面上的HotKey,这个程序将Form1的HotKey设置为
'Ctl+Alt+A.

 

 1 Private  Declare  Function  SendMessage Lib  " user32 "  Alias  " SendMessageA "  (ByVal hwnd  As   Long , ByVal wMsg  As   Long , ByVal wParam  As   Long , lParam  As  Any)  As   Long
 2
 3 Private   Const  WM_SETHOTKEY  =   & H32
 4 Private   Const  HOTKEYF_SHIFT  =   & H1
 5 Private   Const  HOTKEYF_CONTROL  =   & H2
 6 Private   Const  HOTKEYF_ALT  =   & H4
 7
 8 Private   Sub  Form_Load()
 9     Dim  l  As   Long
10     Dim  wHotkey  As   Long
11    
12    wHotkey  =  (HOTKEYF_ALT  Or  HOTKEYF_CONTROL)  *  ( 2   ^   8 +   65
13    l  =  SendMessage(Me.hwnd, WM_SETHOTKEY, wHotkey,  0 )
14 End Sub
15

 

有没有更好的方法呢?????

用键盘钩子,好像可以,但我下载的键盘钩子的代码很多,担心里面可能隐含什么错误~~

高手出招吧~~~~~~~~~~~

http://community.csdn.net/Expert/topic/5278/5278689.xml?temp=.9990045

你可能感兴趣的:(vb)