guys help.. How can i Disable the Left and right Windows key in vb.net??.. my program is on fullscreen and i dont want to interrupt my users with the startup menu when they accedentaly pressed the windows key while they are using my program...
help.. thanks.. ^_^\m/

4 Years
Discussion Span
Last Post by vbDotMe

This is the only way I know of(there is probably a way more effective way with half the code) to do this. This is an old keylogger I wrote about a year ago. I removed half of the functions and set it to block both windows keys... Drop it in your code and tell me what you get. After altering it for you I tested it again and it seems to be working fine.

'Event types
Private Const WM_KEYUP As Integer = &H101
Private Const WM_KEYDOWN As Short = &H100S
Private Const WM_SYSKEYDOWN As Integer = &H104
Private Const WM_SYSKEYUP As Integer = &H105

'Event Info structure
    Public vkCode As Integer
    Public scanCode As Integer
    Public flags As Integer
    Public time As Integer
    Public dwExtraInfo As Integer
End Structure

'Keyboard hook related functions
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Integer) As Integer
Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As KeyboardHookDelegate, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Integer
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As KBDLLHOOKSTRUCT) As Integer
Private Delegate Function KeyboardHookDelegate(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer

Private KeyboardHandle As IntPtr = 0 'Handle of the hook
Private callback As KeyboardHookDelegate = Nothing 'Delegate for the hook

Private Function Hooked()
    Return KeyboardHandle <> 0 'If KeyboardHandle = 0 it means that it isn't hooked so return false, otherwise return true
End Function

Public Sub HookKeyboard()
    callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
    KeyboardHandle = SetWindowsHookEx(13, callback, Process.GetCurrentProcess.MainModule.BaseAddress, 0)
    If KeyboardHandle <> 0 Then
        Debug.Write(vbCrLf & "[Keyboard Hooked]" & vbCrLf)
    End If
End Sub

Public Sub UnhookKeyboard()
    If (Hooked()) Then
        If UnhookWindowsHookEx(KeyboardHandle) <> 0 Then
            Debug.Write(vbCrLf & "[Keyboard Unhooked]")
            KeyboardHandle = 0 'We have unhooked successfully
        End If
    End If
End Sub

Public Function KeyboardCallback(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
    'Variable to hold the text describing the key pressed
    Dim Key = lParam.vkCode

    'If event is KEYDOWN
    If wParam = WM_KEYDOWN Or wParam = WM_SYSKEYDOWN Then
        'If we can block events
        If Code >= 0 Then
            If Key = 91 Or Key = 92 Then
                Return 1 'Block event
            End If
        End If
    End If

    'Return CallNextHookEx(KeyboardHandle, Code, wParam, lParam) 'Let event go to the other applications
End Function

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

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.