您的位置:程序门 -> vb -> 基础类



注册了系统热键,用什么方法激活热键最好?(用timer和死循环就不要了)


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


注册了系统热键,用什么方法激活热键最好?(用timer和死循环就不要了)
发表于:2007-01-06 16:22:11 楼主
我用   registerhotkey   注册了系统热键

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

这个就是用死循环,加了个doevents,防止cpu100%,或者是用了timer控件的

private   const   mod_alt   =   &h1
private   const   mod_control   =   &h2
private   const   mod_shift   =   &h4
private   const   pm_remove   =   &h1
private   const   wm_hotkey   =   &h312
private   type   pointapi
        x   as   long
        y   as   long
end   type
private   type   msg
        hwnd   as   long
        message   as   long
        wparam   as   long
        lparam   as   long
        time   as   long
        pt   as   pointapi
end   type
private   declare   function   registerhotkey   lib   "user32 "   (byval   hwnd   as   long,   byval   id   as   long,   byval   fsmodifiers   as   long,   byval   vk   as   long)   as   long
private   declare   function   unregisterhotkey   lib   "user32 "   (byval   hwnd   as   long,   byval   id   as   long)   as   long
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
private   declare   function   waitmessage   lib   "user32 "   ()   as   long
private   bcancel   as   boolean
private   sub   processmessages()
        dim   message   as   msg
        'loop   until   bcancel   is   set   to   true
        do   while   not   bcancel
                'wait   for   a   message
                waitmessage
                'check   if   it 's   a   hotkey-message
                if   peekmessage(message,   me.hwnd,   wm_hotkey,   wm_hotkey,   pm_remove)   then
                        'minimize   the   form
                        windowstate   =   vbminimized
                end   if
                'let   the   operating   system   process   other   events
                doevents
        loop
end   sub
private   sub   form_load()
        dim   ret   as   long
        bcancel   =   false
        'register   the   ctrl-f   hotkey
        ret   =   registerhotkey(me.hwnd,   &hbfff&,   mod_control,   vbkeyf)
        'show   some   information
        me.autoredraw   =   true
        me.print   "press   ctrl-f   to   minimize   this   form "
        'show   the   form   and
        show
        'process   the   hotkey   messages
        processmessages
end   sub
private   sub   form_unload(cancel   as   integer)
        bcancel   =   true
        'unregister   hotkey
        call   unregisterhotkey(me.hwnd,   &hbfff&)
end   sub

'//第2个示例
'演示怎样设置一个窗口在桌面上的hotkey,这个程序将form1的hotkey设置为
'ctl+alt+a.

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

private   const   wm_sethotkey   =   &h32
private   const   hotkeyf_shift   =   &h1
private   const   hotkeyf_control   =   &h2
private   const   hotkeyf_alt   =   &h4

private   sub   form_load()
      dim   l   as   long
      dim   whotkey   as   long
     
      whotkey   =   (hotkeyf_alt   or   hotkeyf_control)   *   (2   ^   8)   +   65
      l   =   sendmessage(me.hwnd,   wm_sethotkey,   whotkey,   0)
end   sub

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

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

高手出招吧~~~~~~~~~~~
发表于:2007-01-06 16:24:521楼 得分:0
第2个方法不太会用

不知道如何执行自己想要的功能
发表于:2007-01-07 10:53:502楼 得分:0
再顶一下
发表于:2007-01-08 10:00:373楼 得分:0
mark
发表于:2007-01-08 10:12:324楼 得分:0
注册热键的函数,放在模块中:
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
                                shell   "notepad ",   vbnormalfocus
                        end   if
                end   if
        end   if
        '如果不是热键信息则调用原来的程序
        wndproc   =   callwindowproc(prewinproc,   hwnd,   msg,   wparam,   lparam)
       
end   function


使用,放在窗体中:
option   explicit

private   sub   form_load()
dim   ret   as   long

'记录原来的window程序地址
prewinproc   =   getwindowlong(me.hwnd,   gwl_wndproc)
'用自定义程序代替原来的window程序
ret   =   setwindowlong(me.hwnd,   gwl_wndproc,   addressof   wndproc)

idhotkey   =   1
modifiers   =   mod_alt   +   mod_control   'alt+ctrl   键
uvirtkey   =   vbkeyg     'g键
ret   =   registerhotkey(me.hwnd,   idhotkey,   modifiers,   uvirtkey)

end   sub

private   sub   form_unload(cancel   as   integer)
dim   ret   as   long
'取消message的截取,使之送往原来的windows程序
ret   =   setwindowlong(me.hwnd,   gwl_wndproc,   prewinproc)
call   unregisterhotkey(me.hwnd,   uvirtkey)

end   sub


快速检索

最新资讯
热门点击