找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
查看: 6365|回复: 4

[收集的界面] 键盘键值图

  [复制链接]

1214

主题

352

回帖

11

精华

管理员

菜鸟

积分
93755

贡献奖关注奖人气王精英奖乐于助人勋章

发表于 2011-2-10 09:31:42 | 显示全部楼层 |阅读模式
键盘.gif

评分

参与人数 1水晶币 +100 收起 理由
Tesla.Angela + 100

查看全部评分

【VB】QQ群:1422505加的请打上VB好友
【易语言】QQ群:9531809  或 177048
【FOXPRO】QQ群:6580324  或 33659603
【C/C++/VC】QQ群:3777552
【NiceBasic】QQ群:3703755

275

主题

3017

回帖

1

精华

管理员

嗷嗷叫的老马

积分
17064

论坛牛人贡献奖关注奖最佳版主进步奖人气王疯狂作品奖精英奖赞助论坛勋章乐于助人勋章

QQ
发表于 2011-2-10 16:13:25 | 显示全部楼层
哈哈,这个还不错!
我就是嗷嗷叫的老马了......

2

主题

37

回帖

0

精华

铜牌会员

积分
191
发表于 2011-2-10 17:59:28 | 显示全部楼层
VB.net 键盘鼠标全局Hook    BY:夜闻香  http://hi.baidu.com/clso/blog/item/4e6b57b5164d3cc637d3ca64.html
' 夜闻香原创
Form1.vb
  1. Public Class Form1
  2.     Private WithEvents MyHook As New SystemHook()

  3.     Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
  4.         MyHook.UnHook()
  5.     End Sub
  6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  7.         MyHook.StartHook()
  8.         MyHook.KeyHookEnabled = True
  9.     End Sub

  10.     Private Sub MyHook_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyHook.KeyDown
  11.         MsgBox(e.KeyCode)
  12.     End Sub
  13. End Class
复制代码
SystemHook.vb
  1. ''' <summary>本类可以在.NET环境下使用系统键盘与鼠标钩子</summary>
  2. Imports System.Reflection, System.Threading, System.ComponentModel, System.Runtime.InteropServices
  3. Public Class SystemHook

  4. #Region "定义结构"

  5.     Private Structure MouseHookStruct
  6.         Dim PT As Point
  7.         Dim Hwnd As Integer
  8.         Dim WHitTestCode As Integer
  9.         Dim DwExtraInfo As Integer
  10.     End Structure

  11.     Private Structure MouseLLHookStruct
  12.         Dim PT As Point
  13.         Dim MouseData As Integer
  14.         Dim Flags As Integer
  15.         Dim Time As Integer
  16.         Dim DwExtraInfo As Integer
  17.     End Structure

  18.     Private Structure KeyboardHookStruct
  19.         Dim vkCode As Integer
  20.         Dim ScanCode As Integer
  21.         Dim Flags As Integer
  22.         Dim Time As Integer
  23.         Dim DwExtraInfo As Integer
  24.     End Structure

  25. #End Region

  26. #Region "API声明导入"

  27.     Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As HookProc, ByVal hMod As IntPtr, ByVal dwThreadId As Integer) As Integer
  28.     Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal idHook As Integer) As Integer
  29.     Private Declare Function CallNextHookEx Lib "user32" (ByVal idHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
  30.     Private Declare Function ToAscii Lib "user32" (ByVal uVirtKey As Integer, ByVal uScancode As Integer, ByVal lpdKeyState As Byte(), ByVal lpwTransKey As Byte(), ByVal fuState As Integer) As Integer
  31.     Private Declare Function GetKeyboardState Lib "user32" (ByVal pbKeyState As Byte()) As Integer
  32.     Private Declare Function GetKeyState Lib "user32" (ByVal vKey As Integer) As Short

  33.     Private Delegate Function HookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer

  34. #End Region

  35. #Region "常量声明"

  36.     Private Const WH_MOUSE_LL = 14
  37.     Private Const WH_KEYBOARD_LL = 13
  38.     Private Const WH_MOUSE = 7
  39.     Private Const WH_KEYBOARD = 2
  40.     Private Const WM_MOUSEMOVE = &H200
  41.     Private Const WM_LBUTTONDOWN = &H201
  42.     Private Const WM_RBUTTONDOWN = &H204
  43.     Private Const WM_MBUTTONDOWN = &H207
  44.     Private Const WM_LBUTTONUP = &H202
  45.     Private Const WM_RBUTTONUP = &H205
  46.     Private Const WM_MBUTTONUP = &H208
  47.     Private Const WM_LBUTTONDBLCLK = &H203
  48.     Private Const WM_RBUTTONDBLCLK = &H206
  49.     Private Const WM_MBUTTONDBLCLK = &H209
  50.     Private Const WM_MOUSEWHEEL = &H20A
  51.     Private Const WM_KEYDOWN = &H100
  52.     Private Const WM_KEYUP = &H101
  53.     Private Const WM_SYSKEYDOWN = &H104
  54.     Private Const WM_SYSKEYUP = &H105

  55.     Private Const VK_SHIFT As Byte = &H10
  56.     Private Const VK_CAPITAL As Byte = &H14
  57.     Private Const VK_NUMLOCK As Byte = &H90

  58. #End Region

  59. #Region "事件委托处理"

  60.     Private events As New System.ComponentModel.EventHandlerList

  61.     ''' <summary>鼠标激活事件</summary>
  62.     Public Custom Event MouseActivity As MouseEventHandler
  63.         AddHandler(ByVal value As MouseEventHandler)
  64.             events.AddHandler("MouseActivity", value)
  65.         End AddHandler
  66.         RemoveHandler(ByVal value As MouseEventHandler)
  67.             events.RemoveHandler("MouseActivity", value)
  68.         End RemoveHandler
  69.         RaiseEvent(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
  70.             Dim eh As MouseEventHandler = TryCast(events("MouseActivity"), MouseEventHandler)
  71.             If eh IsNot Nothing Then eh.Invoke(sender, e)
  72.         End RaiseEvent
  73.     End Event
  74.     ''' <summary>键盘按下事件</summary>
  75.     Public Custom Event KeyDown As KeyEventHandler
  76.         AddHandler(ByVal value As KeyEventHandler)
  77.             events.AddHandler("KeyDown", value)
  78.         End AddHandler
  79.         RemoveHandler(ByVal value As KeyEventHandler)
  80.             events.RemoveHandler("KeyDown", value)
  81.         End RemoveHandler
  82.         RaiseEvent(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
  83.             Dim eh As KeyEventHandler = TryCast(events("KeyDown"), KeyEventHandler)
  84.             If eh IsNot Nothing Then eh.Invoke(sender, e)
  85.         End RaiseEvent
  86.     End Event
  87.     ''' <summary>键盘输入事件</summary>
  88.     Public Custom Event KeyPress As KeyPressEventHandler
  89.         AddHandler(ByVal value As KeyPressEventHandler)
  90.             events.AddHandler("KeyPress", value)
  91.         End AddHandler
  92.         RemoveHandler(ByVal value As KeyPressEventHandler)
  93.             events.RemoveHandler("KeyPress", value)
  94.         End RemoveHandler
  95.         RaiseEvent(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
  96.             Dim eh As KeyPressEventHandler = TryCast(events("KeyPress"), KeyPressEventHandler)
  97.             If eh IsNot Nothing Then eh.Invoke(sender, e)
  98.         End RaiseEvent
  99.     End Event
  100.     ''' <summary>键盘松开事件</summary>
  101.     Public Custom Event KeyUp As KeyEventHandler
  102.         AddHandler(ByVal value As KeyEventHandler)
  103.             events.AddHandler("KeyUp", value)
  104.         End AddHandler
  105.         RemoveHandler(ByVal value As KeyEventHandler)
  106.             events.RemoveHandler("KeyUp", value)
  107.         End RemoveHandler
  108.         RaiseEvent(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
  109.             Dim eh As KeyEventHandler = TryCast(events("KeyUp"), KeyEventHandler)
  110.             If eh IsNot Nothing Then eh.Invoke(sender, e)
  111.         End RaiseEvent
  112.     End Event

  113. #End Region

  114.     Private hMouseHook As Integer
  115.     Private hKeyboardHook As Integer

  116.     Private Shared MouseHookProcedure As HookProc
  117.     Private Shared KeyboardHookProcedure As HookProc
复制代码

2

主题

37

回帖

0

精华

铜牌会员

积分
191
发表于 2011-2-10 18:26:58 | 显示全部楼层
http://hi.baidu.com/clso/blog/item/4e6b57b5164d3cc637d3ca64.html

续上:
' 夜闻香原创
  1. #Region "创建与析构类型"

  2.     ''' <summary>创建一个全局鼠标键盘钩子 (请使用Start方法开始监视)</summary>
  3.     Sub New()
  4.         '留空即可
  5.     End Sub
  6.     ''' <summary>创建一个全局鼠标键盘钩子,决定是否安装钩子</summary>
  7.     ''' <param name="InstallAll">是否立刻挂钩系统消息</param>
  8.     Sub New(ByVal InstallAll As Boolean)
  9.         If InstallAll Then StartHook(True, True)
  10.     End Sub
  11.     ''' <summary>创建一个全局鼠标键盘钩子,并决定安装钩子的类型</summary>
  12.     ''' <param name="InstallKeyboard">挂钩键盘消息</param>
  13.     ''' <param name="InstallMouse">挂钩鼠标消息</param>
  14.     Sub New(ByVal InstallKeyboard As Boolean, ByVal InstallMouse As Boolean)
  15.         StartHook(InstallKeyboard, InstallMouse)
  16.     End Sub
  17.     ''' <summary>析构函数</summary>
  18.     Protected Overrides Sub Finalize()
  19.         UnHook() '卸载对象时反注册系统钩子
  20.         MyBase.Finalize()
  21.     End Sub

  22. #End Region

  23.     ''' <summary>开始安装系统钩子</summary>
  24.     ''' <param name="InstallKeyboardHook">挂钩键盘消息</param>
  25.     ''' <param name="InstallMouseHook">挂钩鼠标消息</param>
  26.     Public Sub StartHook(Optional ByVal InstallKeyboardHook As Boolean = True, Optional ByVal InstallMouseHook As Boolean = False)
  27.         '注册键盘钩子
  28.         If InstallKeyboardHook AndAlso hKeyboardHook = 0 Then
  29.             KeyboardHookProcedure = New HookProc(AddressOf KeyboardHookProc)
  30.             hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly.GetModules()(0)), 0)
  31.             If hKeyboardHook = 0 Then '检测是否注册完成
  32.                 UnHook(True, False) '在这里反注册
  33.                 'Throw New Win32Exception(Marshal.GetLastWin32Error) '报告错误
  34.             End If
  35.         End If
  36.         '注册鼠标钩子
  37.         If InstallMouseHook AndAlso hMouseHook = 0 Then
  38.             MouseHookProcedure = New HookProc(AddressOf MouseHookProc)
  39.             hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly.GetModules()(0)), 0)
  40.             If hMouseHook = 0 Then
  41.                 UnHook(False, True)
  42.                 Throw New Win32Exception(Marshal.GetLastWin32Error)
  43.             End If
  44.         End If
  45.     End Sub
  46.     ''' <summary>立刻卸载系统钩子</summary>
  47.     ''' <param name="UninstallKeyboardHook">卸载键盘钩子</param>
  48.     ''' <param name="UninstallMouseHook">卸载鼠标钩子</param>
  49.     ''' <param name="ThrowExceptions">是否报告错误</param>
  50.     Public Sub UnHook(Optional ByVal UninstallKeyboardHook As Boolean = True, Optional ByVal UninstallMouseHook As Boolean = True, Optional ByVal ThrowExceptions As Boolean = False)
  51.         '卸载键盘钩子
  52.         If hKeyboardHook <> 0 AndAlso UninstallKeyboardHook Then
  53.             Dim retKeyboard As Integer = UnhookWindowsHookEx(hKeyboardHook)
  54.             hKeyboardHook = 0
  55.             If ThrowExceptions AndAlso retKeyboard = 0 Then '如果出现错误,是否报告错误
  56.                 'Throw New Win32Exception(Marshal.GetLastWin32Error) '报告错误
  57.             End If
  58.         End If
  59.         '卸载鼠标钩子
  60.         If hMouseHook <> 0 AndAlso UninstallMouseHook Then
  61.             Dim retMouse As Integer = UnhookWindowsHookEx(hMouseHook)
  62.             hMouseHook = 0
  63.             If ThrowExceptions AndAlso retMouse = 0 Then
  64.                 'Throw New Win32Exception(Marshal.GetLastWin32Error)
  65.             End If
  66.         End If
  67.     End Sub

  68.     '键盘消息的委托处理代码
  69.     Private Function KeyboardHookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
  70.         Static handled As Boolean : handled = False
  71.         If nCode >= 0 AndAlso (events("KeyDown") IsNot Nothing OrElse events("KeyPress") IsNot Nothing OrElse events("KeyUp") IsNot Nothing) Then
  72.             Static MyKeyboardHookStruct As KeyboardHookStruct
  73.             MyKeyboardHookStruct = DirectCast(Marshal.PtrToStructure(lParam, GetType(KeyboardHookStruct)), KeyboardHookStruct)
  74.             '激活KeyDown
  75.             If wParam = WM_KEYDOWN OrElse wParam = WM_SYSKEYDOWN Then '如果消息为按下普通键或系统键
  76.                 Dim e As New KeyEventArgs(MyKeyboardHookStruct.vkCode)
  77.                 RaiseEvent KeyDown(Me, e) '激活事件
  78.                 handled = handled Or e.Handled '是否取消下一个钩子
  79.             End If
  80.             '激活KeyUp
  81.             If wParam = WM_KEYUP OrElse wParam = WM_SYSKEYUP Then
  82.                 Dim e As New KeyEventArgs(MyKeyboardHookStruct.vkCode)
  83.                 RaiseEvent KeyUp(Me, e)
  84.                 handled = handled Or e.Handled
  85.             End If
  86.             '激活KeyPress (TODO:此段代码还有BUG!)
  87.             If wParam = WM_KEYDOWN Then
  88.                 Dim isDownShift As Boolean = (GetKeyState(VK_SHIFT) & &H80 = &H80)
  89.                 Dim isDownCapslock As Boolean = (GetKeyState(VK_CAPITAL) <> 0)
  90.                 Dim keyState(256) As Byte
  91.                 GetKeyboardState(keyState)
  92.                 Dim inBuffer(2) As Byte
  93.                 If ToAscii(MyKeyboardHookStruct.vkCode, MyKeyboardHookStruct.ScanCode, keyState, inBuffer, MyKeyboardHookStruct.Flags) = 1 Then
  94.                     Static key As Char : key = Chr(inBuffer(0))
  95.                     ' BUG所在
  96.                     'If isDownCapslock Xor isDownShift And Char.IsLetter(key) Then
  97.                     '    key = Char.ToUpper(key)
  98.                     'End If
  99.                     Dim e As New KeyPressEventArgs(key)
  100.                     RaiseEvent KeyPress(Me, e)
  101.                     handled = handled Or e.Handled
  102.                 End If
  103.             End If
  104.             '取消或者激活下一个钩子
  105.             If handled Then Return 1 Else Return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam)
  106.         End If
  107.     End Function
  108.     '鼠标消息的委托处理代码
  109.     Private Function MouseHookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
  110.         If nCode >= 0 AndAlso events("MouseActivity") IsNot Nothing Then
  111.             Static mouseHookStruct As MouseLLHookStruct
  112.             mouseHookStruct = DirectCast(Marshal.PtrToStructure(lParam, GetType(MouseLLHookStruct)), MouseLLHookStruct)
  113.             Static moubut As MouseButtons : moubut = MouseButtons.None '鼠标按键
  114.             Static mouseDelta As Integer : mouseDelta = 0 '滚轮值
  115.             Select Case wParam
  116.                 Case WM_LBUTTONDOWN
  117.                     moubut = MouseButtons.Left
  118.                 Case WM_RBUTTONDOWN
  119.                     moubut = MouseButtons.Right
  120.                 Case WM_MBUTTONDOWN
  121.                     moubut = MouseButtons.Middle
  122.                 Case WM_MOUSEWHEEL
  123.                     Static int As Integer : int = (mouseHookStruct.MouseData >> 16) And &HFFFF
  124.                     '本段代码CLE添加,模仿C#的Short从Int弃位转换
  125.                     If int > Short.MaxValue Then mouseDelta = int - 65536 Else mouseDelta = int
  126.             End Select
  127.             Static clickCount As Integer : clickCount = 0 '单击次数
  128.             If moubut <> MouseButtons.None Then
  129.                 If wParam = WM_LBUTTONDBLCLK OrElse wParam = WM_RBUTTONDBLCLK OrElse wParam = WM_MBUTTONDBLCLK Then
  130.                     clickCount = 2
  131.                 Else
  132.                     clickCount = 1
  133.                 End If
  134.             End If
  135.             Dim e As New MouseEventArgs(moubut, clickCount, mouseHookStruct.PT.X, mouseHookStruct.PT.Y, mouseDelta)
  136.             RaiseEvent MouseActivity(Me, e)
  137.         End If
  138.         Return CallNextHookEx(hMouseHook, nCode, wParam, lParam) '激活下一个钩子
  139.     End Function

  140.     ''' <summary>键盘钩子是否有效</summary>
  141.     Public Property KeyHookEnabled() As Boolean
  142.         Get
  143.             Return hKeyboardHook <> 0
  144.         End Get
  145.         Set(ByVal value As Boolean)
  146.             If value Then StartHook(True, False) Else UnHook(True, False)
  147.         End Set
  148.     End Property
  149.     ''' <summary>鼠标钩子是否有效</summary>
  150.     Public Property MouseHookEnabled() As Boolean
  151.         Get
  152.             Return hMouseHook <> 0
  153.         End Get
  154.         Set(ByVal value As Boolean)
  155.             If value Then StartHook(False, True) Else UnHook(False, True)
  156.         End Set
  157.     End Property

  158. End Class
复制代码

857

主题

2632

回帖

2

精华

管理员

此生无悔入华夏,  长居日耳曼尼亚。  

积分
36130
发表于 2011-2-10 22:29:20 | 显示全部楼层
下载收藏,感谢阿杰叔叔。
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

快速回复 返回顶部 返回列表