klavye

Imports System.Runtime.InteropServices
Imports System.Reflection

Namespace Helper
   Public Module Keyboard

       Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
       Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As IntPtr) As Boolean
       Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As KeyboardHookDelegate, ByVal hmod As IntPtr, ByVal dwThreadId As Integer) As IntPtr
       Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Integer
       Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As IntPtr, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As FMHOOKSTRUCT) As Integer

       Private Const WM_KEYDOWN = &H100
       Private Const WM_KEYUP = &H101

       Public Structure FMHOOKSTRUCT
           Public vkCode As Integer
           Public scanCode As Integer
           Public flags As Integer
           Public time As Integer
           Public dwExtraInfo As Integer
       End Structure

       Private Const HC_ACTION As Integer = 0
       Private Const LLKHF_UP As Integer = &H80

       Public Const VK_CONTROL = &H11
       Private Const WH_KEYBOARD_LL As Integer = 13&
       Public KeyboardHandle As Integer

       Public Function IsHooked(ByRef Hookstruct As FMHOOKSTRUCT) As Boolean
           If Hookstruct.vkCode = VK_CONTROL AndAlso (Hookstruct.flags And LLKHF_UP) = 0 Then
               Return True
           End If
           Return False
       End Function

       Public Function KeyboardCallback(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As FMHOOKSTRUCT) As Integer
           If (Code = HC_ACTION) Then
               If (IsHooked(lParam)) Then
                   Main.LblStatus.Text = "Klavye aktif edildi.."
                   Return 1
               End If
           End If
           Return 0
       End Function

       Public Delegate Function KeyboardHookDelegate(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As FMHOOKSTRUCT) As Integer
       <MarshalAs(UnmanagedType.FunctionPtr)>
       Private callback As KeyboardHookDelegate

       Public Sub HookKeyboard()
           callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
           KeyboardHandle = SetWindowsHookEx(WH_KEYBOARD_LL, callback, Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
           Call CheckHooked()
       End Sub

       Public Sub CheckHooked()
           If (Hooked()) Then
               Main.LblStatus.Text = "Klavye aktif edildi.."
               Main.BtnSetKeyboard.ForeColor = Color.Green
               'Main.Text = "Aktif Edildi!"
           Else
               Main.LblStatus.Text = “Klavye aktif edilemedi: ” & Err.LastDllError
           End If
       End Sub

       Private Function Hooked() As Boolean
           Return KeyboardHandle <> IntPtr.Zero
       End Function

       Public Sub UnhookKeyboard()
           If Hooked() Then
               UnhookWindowsHookEx(KeyboardHandle)
           End If
       End Sub

   End Module

End Namespace