2010年7月12日 星期一

VB.net 設定熱鍵 HotKey(非完整)

Public Const MOD_ALT As Integer = &H1 'Alt key
'宣告必須 API
Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Long) As Integer
Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer

'新增HotKey
Public Overridable Function RegKey(ByRef KeyName As String) As Boolean
If RegisterHotKey(Me.Handle, 29, 0, Keys.Enter) = False Then
KeyName = "Enter"
Return False
End If
If RegisterHotKey(Me.Handle, 1, 0, Keys.F1) = False Then '查詢
KeyName = "F1"
Return False
End If
If RegisterHotKey(Me.Handle, 2, 0, Keys.F2) = False Then '新增
KeyName = "F2"
Return False
End If
If RegisterHotKey(Me.Handle, 3, 0, Keys.F3) = False Then '修改
KeyName = "F3"
Return False
End If
If RegisterHotKey(Me.Handle, 4, 0, Keys.F4) = False Then '刪除
KeyName = "F4"
Return False
End If
If RegisterHotKey(Me.Handle, 5, 0, Keys.F5) = False Then '輸入確認
KeyName = "F5"
Return False
End If
If RegisterHotKey(Me.Handle, 6, 0, Keys.F6) = False Then '取消
KeyName = "F6"
Return False
End If
If RegisterHotKey(Me.Handle, 7, 0, Keys.F7) = False Then
KeyName = "F7"
Return False
End If
If RegisterHotKey(Me.Handle, 8, 0, Keys.F8) = False Then '第一筆
KeyName = "F8"
Return False
End If
If RegisterHotKey(Me.Handle, 9, 0, Keys.F9) = False Then '上一筆
KeyName = "F9"
Return False
End If
If RegisterHotKey(Me.Handle, 10, 0, Keys.F10) = False Then '下一筆
KeyName = "F10"
Return False
End If
If RegisterHotKey(Me.Handle, 11, 0, Keys.F11) = False Then '最後一筆
KeyName = "F11"
Return False
End If
'If RegisterHotKey(Me.Handle, 12, 0, Keys.F12) = False Then '
' KeyName = "F12"
' Return False
'End If
If RegisterHotKey(Me.Handle, 13, MOD_ALT, Keys.F1) = False Then 'detail 查詢
KeyName = "ALT+F1"
Return False
End If
If RegisterHotKey(Me.Handle, 14, MOD_ALT, Keys.F2) = False Then 'detail 新增
KeyName = "ALT+F2"
Return False
End If
If RegisterHotKey(Me.Handle, 15, MOD_ALT, Keys.F3) = False Then 'detail 修改
KeyName = "ALT+F3"
Return False
End If
If RegisterHotKey(Me.Handle, 16, MOD_ALT, Keys.F4) = False Then 'detail 刪除
KeyName = "ALT+F4"
Return False
End If
If RegisterHotKey(Me.Handle, 17, MOD_ALT, Keys.D1) = False Then
KeyName = "ALT+D1"
Return False
End If
If RegisterHotKey(Me.Handle, 18, MOD_ALT, Keys.D2) = False Then
KeyName = "ALT+D2"
Return False
End If
If RegisterHotKey(Me.Handle, 19, MOD_ALT, Keys.D3) = False Then
KeyName = "ALT+D3"
Return False
End If
If RegisterHotKey(Me.Handle, 20, MOD_ALT, Keys.D4) = False Then
KeyName = "ALT+D4"
Return False
End If
If RegisterHotKey(Me.Handle, 21, MOD_ALT, Keys.D5) = False Then
KeyName = "ALT+D5"
Return False
End If
If RegisterHotKey(Me.Handle, 22, MOD_ALT, Keys.D6) = False Then
KeyName = "ALT+D6"
Return False
End If
If RegisterHotKey(Me.Handle, 23, MOD_ALT, Keys.D7) = False Then
KeyName = "ALT+D7"
Return False
End If
If RegisterHotKey(Me.Handle, 24, MOD_ALT, Keys.D8) = False Then
KeyName = "ALT+D8"
Return False
End If
If RegisterHotKey(Me.Handle, 25, MOD_ALT, Keys.D9) = False Then
KeyName = "ALT+D9"
Return False
End If
If RegisterHotKey(Me.Handle, 26, MOD_ALT, Keys.D0) = False Then
KeyName = "ALT+D0"
Return False
End If
Return True
End Function
'解除HotKey
Public Sub UnRegKey()
Call UnregisterHotKey(Me.Handle, 1)
Call UnregisterHotKey(Me.Handle, 2)
Call UnregisterHotKey(Me.Handle, 3)
Call UnregisterHotKey(Me.Handle, 4)
Call UnregisterHotKey(Me.Handle, 5)
Call UnregisterHotKey(Me.Handle, 6)
Call UnregisterHotKey(Me.Handle, 7)
Call UnregisterHotKey(Me.Handle, 8)
Call UnregisterHotKey(Me.Handle, 9)
Call UnregisterHotKey(Me.Handle, 10)
Call UnregisterHotKey(Me.Handle, 11)
Call UnregisterHotKey(Me.Handle, 12)


Call UnregisterHotKey(Me.Handle, 13)
Call UnregisterHotKey(Me.Handle, 14)
Call UnregisterHotKey(Me.Handle, 15)
Call UnregisterHotKey(Me.Handle, 16)

Call UnregisterHotKey(Me.Handle, 17)
Call UnregisterHotKey(Me.Handle, 18)
Call UnregisterHotKey(Me.Handle, 19)
Call UnregisterHotKey(Me.Handle, 20)
Call UnregisterHotKey(Me.Handle, 21)
Call UnregisterHotKey(Me.Handle, 22)
Call UnregisterHotKey(Me.Handle, 23)
Call UnregisterHotKey(Me.Handle, 24)
Call UnregisterHotKey(Me.Handle, 25)
Call UnregisterHotKey(Me.Handle, 26)

Call UnregisterHotKey(Me.Handle, 29)
End Sub
'攔截Windows 訊息
Protected Overloads Overrides Sub WndProc(ByRef m As Message)

'相關資料http://msdn.microsoft.com/zh-tw/library/dd229215.aspx
Const WM_HOTKEY As Integer = &H312
Select Case m.Msg
Case (WM_HOTKEY)
Select Case m.WParam.ToInt32()
Case (1)
F1_KEY()
Case (2)
F2_KEY()
Case (3)
F3_KEY()
Case (4)
F4_KEY()
Case (5)
F5_KEY()
Case (6)
F6_KEY()
Case (7)
F7_KEY()
Case (8)
F8_KEY()
Case (9)
F9_KEY()
Case (10)
F10_KEY()
Case (11)
F11_KEY()
Case (12)
F12_KEY()
Case (13)
ALTF1()
Case (14)
ALTF2()
Case (15)
ALTF3()
Case (16)
ALTF4()
Case (17)
ALTD1()
Case (18)
ALTD2()
Case (19)
ALTD3()
Case (20)
ALTD4()
Case (21)
ALTD5()
Case (22)
ALTD6()
Case (23)
ALTD7()
Case (24)
ALTD8()
Case (25)
ALTD9()
Case (26)
ALTD0()

Case (29)
Enter_KEY()
End Select
Case &H6

If m.WParam.ToInt32() = 1 Or m.WParam.ToInt32() = 2 Then
Dim V_str As String
RegKey(V_str)
'Console.WriteLine("啟動hotkey1")
'Console.WriteLine(V_str)
End If
If m.WParam.ToInt32() = 2097152 Or m.WParam.ToInt32() = 0 Then
UnRegKey()
'Console.WriteLine("移除hotkey1")
End If
'Console.WriteLine(m.WParam.ToInt32() & " @" & Date.Now.ToString("HH:mm:ss"))
End Select

MyBase.WndProc(m)
End Sub

沒有留言:

張貼留言