今天在玩游戏的时候突然有个想法,想想可以用VB.NET全局热键可以实现,现在就看看文章吧。想要一个热键功能~习惯了使用热键...我想想也是...于是就上网找了找VB全局热键的写法... 一看才知道VB.NET全局热键都很占资源大都用DO~LOOP来监视.不喜欢.于是在查阅一些关于GetWindowLong与HotKey的资料后半抄半写的情况下写了以下程序.全局键盘热键。
#T#有统计表明,很大一部分程序缺陷和内存的错误访问有关。正是因为指针直接和内存打交道,所以指针一直以来被看成一个危险的东西。以至于不少语言,如著名的 JAVA,都不提供对指针操作的支持,所有的内存访问方面的处理都由编译器来完成。而象C和C++,指针的使用则是基本功,指针给了程序员极大的自由去随心所欲地处理内存访问,很多非常巧妙的东西都要依靠指针技术来完成。
新建立一个FORM~
- Private Sub Form_Load()
- Dim ret As Long
- preWinProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
- ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf Wndproc)
- idHotKey = 1
- Modifiers = MOD_ALT + MOD_CONTROL
- uVirtKey = vbKeyA
- ret = RegisterHotKey(Me.hwnd, idHotKey, Modifiers, uVirtKey)
- End Sub
- Private Sub Form_Unload(Cancel As Integer)
- Dim ret As Long
- ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, preWinProc)
- Call UnregisterHotKey(Me.hwnd, uVirtKey)
- End Sub
新建立一个模块
- 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
- msgbox "你按下了热键哦~"
- End If
- End If
- End If
- Wndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)
- End Function