Microsoft 开发人员网络 > 论坛主页 > Visual Basic IDE > How do you disable the alt key?????
提出问题提出问题
 

已答复How do you disable the alt key?????

答案

  • 2007年8月8日 11:44James A. Gayhart 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    Hi,

     

    I hope that this will get you started; watch for word wrap:

     

    Code Snippet

    Imports System.Runtime.InteropServices

    Imports System.Reflection

    Module mKeyboard

    #Region "Declarations"

        Private Const HC_ACTION As Integer = 0

        Private Const WH_KEYBOARD_LL As Integer = 13&

     

        Private Structure KBDLLHookStruct

            Public vkCode As Integer

            Public scanCode As Integer

            Public flags As Integer

            Public time As Integer

            Public dwExtraInfo As Integer

        End Structure

     

        <MarshalAs(UnmanagedType.FunctionPtr)> _

        Private callback As KeyboardHookDelegate

        Private mbBlockKeys As Boolean = True

        Private miKeyboardHandle As Integer = 0

     

        Private Delegate Function KeyboardHookDelegate(ByVal Code As Integer, _

            ByVal wParam As Integer, ByRef lParam As KBDLLHookStruct) 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 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 UnhookWindowsHookEx Lib "user32" ( _

            ByVal hHook As Integer) As Integer

    #End Region

     

    #Region "Properties"

        Public Property BlockKeyCombinations() As Boolean

            Get

                Return mbBlockKeys

            End Get

            Set(ByVal value As Boolean)

                mbBlockKeys = value

            End Set

        End Property

     

        Public ReadOnly Property IsHooked() As Boolean

            Get

                Return miKeyboardHandle <> 0

            End Get

        End Property

    #End Region

     

        Public Function IsInIDE() As Boolean

            Return System.Diagnostics.Debugger.IsAttached

        End Function

     

        Public Sub HookKeyboard()

            ' Release any existing keyboard hook.

            UnhookKeyboard()

     

            If Not IsInIDE() Then

                callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)

                miKeyboardHandle = SetWindowsHookEx(WH_KEYBOARD_LL, callback, _

                Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)

            End If

        End Sub

     

        Public Sub UnhookKeyboard()

            If (IsHooked()) Then Call UnhookWindowsHookEx(miKeyboardHandle)

        End Sub

     

        Private Function BlockKeyCombination( _

            ByVal Hookstruct As KBDLLHookStruct) As Boolean

     

            Dim bResult As Boolean = False

     

            If mbBlockKeys Then

                Select Case Hookstruct.vkCode

                    Case System.ConsoleKey.Escape

                        If My.Computer.Keyboard.CtrlKeyDown Then

                            Debug.Print("Blocking: Ctrl-Esc")

                            bResult = True

                        ElseIf My.Computer.Keyboard.AltKeyDown Then

                            Debug.Print("Blocking: Alt-Esc")

                            bResult = True

                        End If

                    Case System.ConsoleKey.Tab

                        If My.Computer.Keyboard.AltKeyDown Then

                            Debug.Print("Blocking: Alt-Tab")

                            bResult = True

                        End If

                    Case System.ConsoleKey.RightWindows, System.ConsoleKey.LeftWindows

                        Debug.Print("Blocking: Windows Key")

                        bResult = True

                    Case System.ConsoleKey.Applications

                        Debug.Print("Blocking: Application Key")

                        bResult = True

                    Case System.ConsoleKey.F4

                        If My.Computer.Keyboard.AltKeyDown Then

                            Debug.Print("Blocking: Alt-F4")

                            bResult = True

                        End If

                    Case Else

                End Select

            End If

     

            Return bResult

        End Function

     

        Private Function KeyboardCallback(ByVal Code As Integer, _

            ByVal wParam As Integer, ByRef lParam As KBDLLHookStruct) As Integer

     

            Dim lResult As Integer = 0

            If (Code = HC_ACTION) AndAlso (BlockKeyCombination(lParam)) Then

                lResult = 1

            Else

                lResult = CallNextHookEx(miKeyboardHandle, Code, wParam, lParam)

            End If

     

            Return lResult

        End Function

    End Module

     

     

全部回复

  • 2007年8月6日 15:09Anarchy 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    I'll tell you, but only if you PROMISE you aren't going to write an app which is so so self-important that it thinks it must be always on top

  • 2007年8月8日 11:44James A. Gayhart 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    Hi,

     

    I hope that this will get you started; watch for word wrap:

     

    Code Snippet

    Imports System.Runtime.InteropServices

    Imports System.Reflection

    Module mKeyboard

    #Region "Declarations"

        Private Const HC_ACTION As Integer = 0

        Private Const WH_KEYBOARD_LL As Integer = 13&

     

        Private Structure KBDLLHookStruct

            Public vkCode As Integer

            Public scanCode As Integer

            Public flags As Integer

            Public time As Integer

            Public dwExtraInfo As Integer

        End Structure

     

        <MarshalAs(UnmanagedType.FunctionPtr)> _

        Private callback As KeyboardHookDelegate

        Private mbBlockKeys As Boolean = True

        Private miKeyboardHandle As Integer = 0

     

        Private Delegate Function KeyboardHookDelegate(ByVal Code As Integer, _

            ByVal wParam As Integer, ByRef lParam As KBDLLHookStruct) 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 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 UnhookWindowsHookEx Lib "user32" ( _

            ByVal hHook As Integer) As Integer

    #End Region

     

    #Region "Properties"

        Public Property BlockKeyCombinations() As Boolean

            Get

                Return mbBlockKeys

            End Get

            Set(ByVal value As Boolean)

                mbBlockKeys = value

            End Set

        End Property

     

        Public ReadOnly Property IsHooked() As Boolean

            Get

                Return miKeyboardHandle <> 0

            End Get

        End Property

    #End Region

     

        Public Function IsInIDE() As Boolean

            Return System.Diagnostics.Debugger.IsAttached

        End Function

     

        Public Sub HookKeyboard()

            ' Release any existing keyboard hook.

            UnhookKeyboard()

     

            If Not IsInIDE() Then

                callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)

                miKeyboardHandle = SetWindowsHookEx(WH_KEYBOARD_LL, callback, _

                Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)

            End If

        End Sub

     

        Public Sub UnhookKeyboard()

            If (IsHooked()) Then Call UnhookWindowsHookEx(miKeyboardHandle)

        End Sub

     

        Private Function BlockKeyCombination( _

            ByVal Hookstruct As KBDLLHookStruct) As Boolean

     

            Dim bResult As Boolean = False

     

            If mbBlockKeys Then

                Select Case Hookstruct.vkCode

                    Case System.ConsoleKey.Escape

                        If My.Computer.Keyboard.CtrlKeyDown Then

                            Debug.Print("Blocking: Ctrl-Esc")

                            bResult = True

                        ElseIf My.Computer.Keyboard.AltKeyDown Then

                            Debug.Print("Blocking: Alt-Esc")

                            bResult = True

                        End If

                    Case System.ConsoleKey.Tab

                        If My.Computer.Keyboard.AltKeyDown Then

                            Debug.Print("Blocking: Alt-Tab")

                            bResult = True

                        End If

                    Case System.ConsoleKey.RightWindows, System.ConsoleKey.LeftWindows

                        Debug.Print("Blocking: Windows Key")

                        bResult = True

                    Case System.ConsoleKey.Applications

                        Debug.Print("Blocking: Application Key")

                        bResult = True

                    Case System.ConsoleKey.F4

                        If My.Computer.Keyboard.AltKeyDown Then

                            Debug.Print("Blocking: Alt-F4")

                            bResult = True

                        End If

                    Case Else

                End Select

            End If

     

            Return bResult

        End Function

     

        Private Function KeyboardCallback(ByVal Code As Integer, _

            ByVal wParam As Integer, ByRef lParam As KBDLLHookStruct) As Integer

     

            Dim lResult As Integer = 0

            If (Code = HC_ACTION) AndAlso (BlockKeyCombination(lParam)) Then

                lResult = 1

            Else

                lResult = CallNextHookEx(miKeyboardHandle, Code, wParam, lParam)

            End If

     

            Return lResult

        End Function

    End Module

     

     

  • 2007年10月16日 13:51JustAnotherITGuy 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    James,

     

    I am having a very similar issue to Simone1.  I will try this out and see if it addresses the problem.

     

    Thank you for the help...

     

  • 2007年10月16日 14:40JustAnotherITGuy 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    James,

     

    I got the code into my program, but what do I do with it?  What do I call/pass so that input is checked?

     

    I have it as a module available to the whole program.  I can call the HookKeyboard public sub - but when I debug the code, pressing the keys that your code should trap, nothing happens...

     

  • 2007年10月16日 15:08James A. Gayhart 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    Hi,


    Call mKeyboard.HookKeyboard in the application startup. 

    Call mKeyboard.UnhookKeyboard when shutting down.

    Change mKeyboard.BlockKeyCombination to disallow key combinations.

    Set mKeyboard.BlockKeyCombinations to True to block the defined key combinations.

     

    That should get you going.  Keep in mind that HookKeyboard will not install a new keyboard hook when running in Debug mode.  Try running in release mode.

     

    If you are still stuck, please let me know.

  • 2007年10月16日 15:43HanneSThEGreaTMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
     kandaras0 wrote:

    Hi, i'm new at this, how would you stop someone using the alt key, like alt+F4, alt+ctrl+delete OR alt+tab

     

    You can perhaps aslo use the SystemParametersInfo API here.  This API will block all these keys ( except for Alt + Ctrl + Del ).  The reaon for this is that whether or not you specify it, the Task manager ( on Windows XP ) will still show.

    That will be your biggest headache.

    You can disable the Taskmanager by adding a Timer to your Form, and keep bringing your form to the fron ( ok, it won't disable tha taskmanager, but it will hide it )

    To really disable the Taskmanager from showing by editing the registry key :

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\LogonType

     

    or you could get the Taskmanager's Process Id with the GetWindowThreadProcessId API, then terminate it with the TerminateProcess API.

     

    To be honest.  I'm not if any of my comments were illegal in this forum, so mods, feel free to edit / delet as you see fit.

     

    Another comment kandaras, is that it is very impractical and illogical  why anyone would need to hide the taskmanager completely.  The Task Manager exists for a reason.

  • 2007年10月16日 16:54James A. Gayhart 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

     HanneSThEGreaT wrote:

    Another comment kandaras, is that it is very impractical and illogical  why anyone would need to hide the taskmanager completely.  The Task Manager exists for a reason.

     

    I agree that most applications should not prevent Task Manager from popping up nor should they block key combinations.  Instead, policies are probably a better way to go.  In my particular case, the applications I write control machinery.  Forcibly killing the application or doing other tasks in Windows could cause machine malfunctions.  Since we do not always have have access to change policies, it was easier/quicker to disable "stuff" within the application.  That being said, we do allow that ability to be configured by user access (operators can't exit the app, but admins can for example).

     

    I am, however, always open to different solutions.

  • 2008年8月29日 21:44Aybe81 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Hello James,


    I had to change the visibility of BlockKeyCombination to Public because i couldn't call it from my window.

    Also, when i call the BlockKeyCombination function, no parameters pulls out thru IntelliSense.

    What should i specify in the KBDLLHookStruct structure ?


    I have seen this :

    KBDLLHOOKSTRUCT Structure
    http://msdn.microsoft.com/en-us/library/ms644967(VS.85).aspx

    But it doesn't tell what values are for the desired key ... etc


    Help please,

    Thank you !