免費論壇 繁體 | 簡體
Sclub交友聊天~加入聊天室當版主
分享
返回列表 回復 發帖

快速鍵(熱鍵)使用範例

Imports System.Runtime.InteropServices

Public Class Form1
Private Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
Private Declare Sub UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer)
Private Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpStr As String) As Short
Private Declare Function GlobalDeleteAtom Lib "kernel32" (ByVal nAtom As Short) As Short

Private Const MOD_ALT As Integer = 1
Private Const MOD_CONTROL As Integer = 2
Private Const MOD_SHIFT As Integer = 4
Private Const MOD_WIN As Integer = 8
Private count As Integer = 0

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  RegisterGlobalHotKey(Keys.F5, MOD_SHIFT, 0)
  RegisterGlobalHotKey(Keys.F6, MOD_SHIFT, 1)
  RegisterGlobalHotKey(Keys.F7, MOD_SHIFT, 2)
  RegisterGlobalHotKey(Keys.F8, MOD_SHIFT, 3)
End Sub

Public Sub UnregisterGlobalHotKey()
  For i = 0 To 3
   If Me.hotkeyID(i) <> 0 Then
    UnregisterHotKey(Me.Handle, hotkeyID(i))
    ' clean up the atom list
    GlobalDeleteAtom(hotkeyID(i))
    hotkeyID(i) = 0
   End If
  Next
End Sub
' the id for the hotkey
Dim hotkeyID(4) As Short
' register a global hot key
Public Sub RegisterGlobalHotKey(ByVal hotkey As Keys, ByVal modifiers As Integer, ByVal iID As Integer)
  Try
   ' use the GlobalAddAtom API to get a unique ID (as suggested by MSDN docs
   hotkeyID(iID) = GlobalAddAtom(iID.ToString())
   If hotkeyID(iID) = 0 Then
    Throw New Exception("Unable to generate unique hotkey ID. Error code: " & _
    Marshal.GetLastWin32Error().ToString)
   End If

   ' register the hotkey, throw if any error
   If RegisterHotKey(Me.Handle, hotkeyID(iID), modifiers, CInt(hotkey)) = 0 Then
    Throw New Exception("Unable to register hotkey. Error code: " & _
    Marshal.GetLastWin32Error.ToString)
   End If
  Catch ex As Exception
   ' clean up if hotkey registration failed
   UnregisterGlobalHotKey()
  End Try
End Sub
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
  ' let the base class process the message
  MyBase.WndProc(m)
  ' if this is a WM_HOTKEY message, notify the parent object
  Const WM_HOTKEY As Integer = &H312

  If m.Msg = WM_HOTKEY Then
   If m.WParam = hotkeyID(0) Then
    MessageBox.Show("Shift + F5")
   ElseIf m.WParam = hotkeyID(1) Then
    MessageBox.Show("Shift + F6")
   ElseIf m.WParam = hotkeyID(2) Then
    MessageBox.Show("Shift + F7")
   ElseIf m.WParam = hotkeyID(3) Then
    MessageBox.Show("Shift + F8")
   End If
   'count += 1
   'Label1.Text = count
  End If
End Sub

Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
  UnregisterGlobalHotKey()
End Sub
End Class
----------------------------------------------------------------------------
參考
今天在MSDN上看到下面這篇文章VB2005按鍵問題;主要是說如何在程式沒有得到焦點的情形下還能接收到我們設定的按鍵;感覺還滿有趣的,根據璉大文中提供的資料去看了一下也順便實際操作一下,還滿順利的,貼出程式碼供大家參考看看了
首先是一些相關的參考資料



.NET system wide hotkey component
按下某組鍵(HotKey)便執行某程式


下面是參考的程式碼
Module的程式碼
Imports System.Runtime.InteropServices

Module Module1
    <DllImport("user32", EntryPoint:="RegisterHotKey", _
        SetLastError:=True, _
        ExactSpelling:=True, _
        CallingConvention:=CallingConvention.StdCall)> _
    Public Function RegisterHotkey(ByVal hwnd As IntPtr, _
               ByVal Id As Int32, _
               <MarshalAs(UnmanagedType.U4)> ByVal fsModifiers As Int32, _
               <MarshalAs(UnmanagedType.U4)> ByVal vkey As Int32) As Boolean

    End Function

    <DllImport("user32", EntryPoint:="UnregisterHotKey", _
        SetLastError:=True, _
        ExactSpelling:=True, _
        CallingConvention:=CallingConvention.StdCall)> _
    Public Function UnregisterHotkey(ByVal hwnd As Int32, _
                                ByVal Id As Int32) As Boolean

    End Function

    <DllImport("kernel32", EntryPoint:="GlobalAddAtom", _
        SetLastError:=True, _
        ExactSpelling:=False)> _
    Public Function GlobalAddAtom(<MarshalAs(UnmanagedType.LPTStr)> _
                ByVal lpString As String) As Int32

    End Function

    Public Enum HotkeyModifierFlags
        MOD_ALT = &H1
        MOD_CONTROL = &H2
        MOD_SHIFT = &H4
        MOD_WIN = &H8
    End Enum
End Module

Form的程式碼(Form上面有一個button1)





view source

print?


01 Public Class Form1


02     Dim myID, myID1 As Integer


03     Public Const WM_HOTKEY = &H312


04   


05     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


06         Try


07             myID = GlobalAddAtom("TEST")


08             If Not RegisterHotkey(MyBase.Handle, myID, HotkeyModifierFlags.MOD_CONTROL, Keys.A) Then


09             End If


10             myID1 = GlobalAddAtom("12345")


11             If Not RegisterHotkey(MyBase.Handle, myID1, HotkeyModifierFlags.MOD_CONTROL, Keys.B) Then


12             End If


13         Catch ex As Exception


14             System.Diagnostics.Debug.WriteLine(ex.ToString)


15         End Try


16     End Sub


17   


18     Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing


19         UnregisterHotkey(MyBase.Handle, myID)


20         UnregisterHotkey(MyBase.Handle, myID1)


21     End Sub


22   


23     Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)


24         Select Case m.Msg


25             Case WM_HOTKEY


26                 If m.WParam = myID Then


27                     MessageBox.Show("ctrl+a")


28                 ElseIf m.WParam = myID1 Then


29                     MessageBox.Show("ctrl+b")


30                 End If


31         End Select


32         MyBase.WndProc(m)


33     End Sub


34 End Class
返回列表