Advertisement
TheVideoVolcano

Keyboard

Mar 22nd, 2013
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.96 KB | None | 0 0
  1. Public Class Keyboard
  2.     Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal Hook As Integer, ByVal KeyDelegate As KDel, ByVal HMod As Integer, ByVal ThreadId As Integer) As Integer
  3.     Private Declare Function CallNextHookEx Lib "user32" (ByVal Hook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As KeyStructure) As Integer
  4.     Private Declare Function UnhookWindowsHookEx Lib "user32" Alias "UnhookWindowsHookEx" (ByVal Hook As Integer) As Integer
  5.     Private Delegate Function KDel(ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As KeyStructure) As Integer
  6.     Public Shared Event Down(ByVal Key As String)
  7.     Public Shared Event Up(ByVal Key As String)
  8.     Private Shared Key As Integer
  9.     Private Shared KHD As KDel
  10.     Private Structure KeyStructure : Public Code As Integer : Public ScanCode As Integer : Public Flags As Integer : Public Time As Integer : Public ExtraInfo As Integer : End Structure
  11.     Public Sub CreateHook()
  12.         KHD = New KDel(AddressOf Proc)
  13.         Key = SetWindowsHookEx(13, KHD, System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
  14.     End Sub
  15.  
  16.     Private Function Proc(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KeyStructure) As Integer
  17.         If (Code = 0) Then
  18.             Select Case wParam
  19.                 Case &H100, &H104 : RaiseEvent Down(Feed(CType(lParam.Code, Keys)))
  20.                 Case &H101, &H105 : RaiseEvent Up(Feed(CType(lParam.Code, Keys)))
  21.             End Select
  22.         End If
  23.         Return CallNextHookEx(Key, Code, wParam, lParam)
  24.     End Function
  25.     Public Sub DiposeHook()
  26.         UnhookWindowsHookEx(Key)
  27.         MyBase.Finalize()
  28.     End Sub
  29.     Private Function Feed(ByVal e As Keys) As String
  30.         Select Case e
  31.             Case 65 To 90
  32.                 If Control.IsKeyLocked(Keys.CapsLock) Or (Control.ModifierKeys And Keys.Shift) <> 0 Then
  33.                     Return e.ToString
  34.                 Else
  35.                     Return e.ToString.ToLower
  36.                 End If
  37.             Case 48 To 57
  38.                 If (Control.ModifierKeys And Keys.Shift) <> 0 Then
  39.                     Select Case e.ToString
  40.                         Case "D1" : Return "!"
  41.                         Case "D2" : Return "@"
  42.                         Case "D3" : Return "#"
  43.                         Case "D4" : Return "$"
  44.                         Case "D5" : Return "%"
  45.                         Case "D6" : Return "^"
  46.                         Case "D7" : Return "&"
  47.                         Case "D8" : Return "*"
  48.                         Case "D9" : Return "("
  49.                         Case "D0" : Return ")"
  50.                     End Select
  51.                 Else
  52.                     Return e.ToString.Replace("D", Nothing)
  53.                 End If
  54.             Case 96 To 105
  55.                 Return e.ToString.Replace("NumPad", Nothing)
  56.             Case 106 To 111
  57.                 Select Case e.ToString
  58.                     Case "Divide" : Return "/"
  59.                     Case "Multiply" : Return "*"
  60.                     Case "Subtract" : Return "-"
  61.                     Case "Add" : Return "+"
  62.                     Case "Decimal" : Return "."
  63.                 End Select
  64.             Case 32
  65.                 Return " "
  66.             Case 186 To 222
  67.                 If (Control.ModifierKeys And Keys.Shift) <> 0 Then
  68.                     Select Case e.ToString
  69.                         Case "OemMinus" : Return "_"
  70.                         Case "Oemplus" : Return "+"
  71.                         Case "OemOpenBrackets" : Return "{"
  72.                         Case "Oem6" : Return "}"
  73.                         Case "Oem5" : Return "|"
  74.                         Case "Oem1" : Return ":"
  75.                         Case "Oem7" : Return """"
  76.                         Case "Oemcomma" : Return "<"
  77.                         Case "OemPeriod" : Return ">"
  78.                         Case "OemQuestion" : Return "?"
  79.                         Case "Oemtilde" : Return "~"
  80.                     End Select
  81.                 Else
  82.                     Select Case e.ToString
  83.                         Case "OemMinus" : Return "-"
  84.                         Case "Oemplus" : Return "="
  85.                         Case "OemOpenBrackets" : Return "["
  86.                         Case "Oem6" : Return "]"
  87.                         Case "Oem5" : Return "\"
  88.                         Case "Oem1" : Return ";"
  89.                         Case "Oem7" : Return "'"
  90.                         Case "Oemcomma" : Return ","
  91.                         Case "OemPeriod" : Return "."
  92.                         Case "OemQuestion" : Return "/"
  93.                         Case "Oemtilde" : Return "'"
  94.                     End Select
  95.                 End If
  96.             Case Keys.Return
  97.                 Return Environment.NewLine
  98.             Case Else
  99.                 Return "<" + e.ToString + ">"
  100.         End Select
  101.         Return Nothing
  102.     End Function
  103. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement