9 Replies - 21287 Views - Last Post: 17 June 2010 - 05:24 AM Rate Topic: -----

#1 fenrisulfr  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 16-August 08

Disabling keys in a application

Posted 08 September 2008 - 06:05 PM

is there a way to disable keys on a keyboard?

like the windows key? ... i hate that key in games :(

This post has been edited by fenrisulfr: 08 September 2008 - 06:24 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Disabling keys in a application

#2 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: Disabling keys in a application

Posted 08 September 2008 - 08:38 PM

You can disable keys, but for some reason you can not disable the windows key.

Here is how you disable the space key
Put this in the KeyDown() event
If e.KeyCode = Keys.Space Then
	e.SuppressKeyPress = True
End If


This post has been edited by gbertoli3: 08 September 2008 - 08:39 PM

Was This Post Helpful? 0
  • +
  • -

#3 Damage  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 27
  • View blog
  • Posts: 1,079
  • Joined: 05-June 08

Re: Disabling keys in a application

Posted 08 September 2008 - 11:42 PM

google it, theres plenty of people that get just as pissed off about it :P

here's first on the list
http://johnhaller.co...le_windows_key/
Was This Post Helpful? 0
  • +
  • -

#4 RodgerB  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 66
  • View blog
  • Posts: 2,284
  • Joined: 21-September 07

Re: Disabling keys in a application

Posted 09 September 2008 - 02:57 AM

I decided to give this a try. What this code will do is, if the form detects a key press from the windows key, it will press the Zoom key (I don't know what this is, so I assume it's not important) instead, and counteract the windows key press.

Public Class Form1

    Private Const KEYEVENTF_EXTENDEDKEY As Long = &H1
    Private Const KEYEVENTF_KEYUP As Long = &H2
    Private Const VK_LWIN As Byte = &H5B
    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
    ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

    Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.LWin Or e.KeyCode = Keys.RWin Then keybd_event(Keys.Zoom, 0, KEYEVENTF_EXTENDEDKEY, 0)
    End Sub

End Class


I decided to take it further, because the poster of the topic wanted to make sure it didn't happen in the middle of a game. The above code will only call if the form is in focus. Below is some API code i made to intercept the key press and then press the zoom key, but without the form needing focus.

Option Strict On
Imports System.Runtime.InteropServices

Public Class Form1

    Private Const KEYEVENTF_EXTENDEDKEY As Long = &H1
    Private Const KEYEVENTF_KEYUP As Long = &H2
    Private Const VK_LWIN As Byte = &H5B
    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
    ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

    Private Const WH_KEYBOARD_LL As Integer = 13
    Private Const WM_KEYUP As Integer = &H101
    Private Shared _proc As LowLevelKeyboardProc = AddressOf HookCallback
    Private Shared _hookID As IntPtr = IntPtr.Zero

    Public Declare Auto Function SetWindowsHookEx Lib "user32.dll" ( _
        ByVal idHook As Integer, ByVal lpfn As LowLevelKeyboardProc, _
        ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr

    Public Declare Auto Function UnhookWindowsHookEx _
    Lib "user32.dll" (ByVal hhk As IntPtr) As IntPtr

    Public Declare Auto Function CallNextHookEx _
    Lib "user32.dll" (ByVal hhk As IntPtr, ByVal nCode As Integer, _
                      ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

    Public Declare Auto Function GetModuleHandle Lib "kernel32.dll" ( _
    ByVal lpModuleName As String) As IntPtr


    Private Shared Function SetHook( _
        ByVal proc As LowLevelKeyboardProc) As IntPtr

        Dim curProcess As Process = Process.GetCurrentProcess()
        Dim curModule As ProcessModule = curProcess.MainModule

        Return SetWindowsHookEx(WH_KEYBOARD_LL, proc, _
                GetModuleHandle(curModule.ModuleName), 0)

    End Function

    Public Delegate Function LowLevelKeyboardProc( _
        ByVal nCode As Integer, ByVal wParam As IntPtr, _
        ByVal lParam As IntPtr) As IntPtr

    Public Shared Function HookCallback( _
        ByVal nCode As Integer, _
        ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

        If nCode >= 0 And wParam = CType(WM_KEYUP, IntPtr) Then
            Dim vkCode As Keys = CType(Marshal.ReadInt32(lParam), Keys)
            If vkCode = Keys.LWin Or vkCode = Keys.RWin Then
                keybd_event(CByte(Keys.Zoom), 0, KEYEVENTF_EXTENDEDKEY, 0)
            End If
        End If

        Return CallNextHookEx(_hookID, nCode, wParam, lParam)
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        _hookID = SetHook(_proc)
    End Sub

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        UnhookWindowsHookEx(_hookID)
    End Sub

End Class


Damage's post does seem like a good idea, but the executer will require administrator access to the registry (edit: in his example). Hope that helps. :)

This post has been edited by RodgerB: 09 September 2008 - 02:58 AM

Was This Post Helpful? 1
  • +
  • -

#5 Damage  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 27
  • View blog
  • Posts: 1,079
  • Joined: 05-June 08

Re: Disabling keys in a application

Posted 09 September 2008 - 03:39 AM

plus it's always cool to know how to do things yourself :P
Was This Post Helpful? 0
  • +
  • -

#6 fenrisulfr  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 16-August 08

Re: Disabling keys in a application

Posted 09 September 2008 - 11:27 AM

I recently started VB and i always google it..

unfortunatly most of it is over my head and this forum seems to explain questions the best.

Thanks Guys!
Was This Post Helpful? 0
  • +
  • -

#7 octane  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 04-November 09

Re: Disabling keys in a application

Posted 08 November 2009 - 08:46 PM

can i also disable ctrl+esc with this code? Iam trying to disable shortcuts to the Startmenu.

i added this code but it doesn't seem to work.
 If e.KeyCode = Keys.LWin Or e.KeyCode = Keys.RWin Then keybd_event(Keys.Zoom, 0, KEYEVENTF_EXTENDEDKEY, 0)
If e.KeyCode = Keys.ControlKey And e.Modifiers = Keys.Escape Then keybd_event(Keys.Zoom, 0, KEYEVENTF_EXTENDEDKEY, 0)



[quote name='RodgerB' date='9 Sep, 2008 - 01:57 AM' post='415186']
I decided to give this a try. What this code will do is, if the form detects a key press from the windows key, it will press the Zoom key (I don't know what this is, so I assume it's not important) instead, and counteract the windows key press.

Public Class Form1

    Private Const KEYEVENTF_EXTENDEDKEY As Long = &H1
    Private Const KEYEVENTF_KEYUP As Long = &H2
    Private Const VK_LWIN As Byte = &H5B
    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
    ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

    Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.LWin Or e.KeyCode = Keys.RWin Then keybd_event(Keys.Zoom, 0, KEYEVENTF_EXTENDEDKEY, 0)
    End Sub

End Class

Was This Post Helpful? 0
  • +
  • -

#8 kevinrich47  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-March 10

Re: Disabling keys in a application

Posted 04 March 2010 - 07:52 AM

You don't have to write code for it,
there are some software could do this,
here is one of it
Disable Key

This post has been edited by kevinrich47: 04 March 2010 - 07:53 AM

Was This Post Helpful? 0
  • +
  • -

#9 Guest_Aymen AlAnsari*


Reputation:

Re: Disabling keys in a application

Posted 16 June 2010 - 11:15 PM

Try this link. There are registry keys that disable windows key. Worked on Windows 7 for me.
http://johnhaller.co...le_windows_key/

If you want to include it in an application, here is my code:
        Dim regKey As RegistryKey
        regKey = Registry.LocalMachine.OpenSubKey("System\CurrentControlSet\Control\Keyboard Layout", True)
        Try
            Dim data As Byte() = {&H0, &H0, &H0, &H0, &H0, &H0, &H0, _
                                  &H0, &H3, &H0, &H0, &H0, &H0, &H0, &H5B, &HE0 _
                                  , &H0, &H0, &H5C, &HE0, &H0, &H0, &H0, &H0}
            regKey.SetValue("Scancode Map", data, RegistryValueKind.Binary)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


To delete the registry key when you finish, you can use this code:
        regKey = Registry.LocalMachine.OpenSubKey("System\CurrentControlSet\Control\Keyboard Layout", True)
        Try
            regKey.DeleteValue("Scancode Map", True)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


Notice that you will have to log off and in again for changes to take effect.
Was This Post Helpful? 0

#10 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: Disabling keys in a application

Posted 17 June 2010 - 05:24 AM

Hi,

For disable the Left and Right Windows keys, try this:

Private Declare Function SetWindowsHookEx _
 Lib "user32" Alias "SetWindowsHookExA" ( _
 ByVal idHook As Long, _
 ByVal lpfn As Long, _
 ByVal hmod As Long, _
 ByVal dwThreadId As Long) As Long
  
Private Declare Function CallNextHookEx Lib "user32" ( _
 ByVal hHook As Long, _
 ByVal nCode As Long, _
 ByVal wParam As Long, _
 ByVal lParam As Long) As Long

Private Declare Function UnhookWindowsHookEx Lib "user32" ( _
 ByVal hHook As Long) As Long

Private Declare Sub CopyMemory _
 Lib "kernel32" Alias "RtlMoveMemory" ( _
 pDest As Any, _
 pSource As Any, _
 ByVal cb As Long)

Private Type KBDLLHOOKSTRUCT
    vkCode As Long
    scanCode As Long
    flags As Long
    time As Long
    dwExtraInfo As Long
End Type

Private Const HC_ACTION = 0&
Private Const WH_KEYBOARD_LL = 13&
Private Const VK_LWIN = &H5B&
Private Const VK_RWIN = &H5C&

Private hKeyb As Long

Private Function KeybCallback(ByVal Code As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Static udtHook As KBDLLHOOKSTRUCT
    
    If (Code = HC_ACTION) Then
        'Copy the keyboard data out of the lParam (which is a pointer)
        Call CopyMemory(udtHook, ByVal lParam, Len(udtHook))
        Select Case udtHook.vkCode
            Case VK_LWIN, VK_RWIN
                KeybCallback = 1
                Exit Function
        End Select
    End If
    KeybCallback = CallNextHookEx(hKeyb, Code, wParam, lParam)
End Function

Public Sub HookKeyboard()
    UnhookKeyboard
    hKeyb = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf KeybCallback, App.hInstance, 0&)
End Sub

Public Sub UnhookKeyboard()
    If hKeyb <> 0 Then
        Call UnhookWindowsHookEx(hKeyb)
        hKeyb = 0
    End If
End Sub

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1