locked
Need to capture ALT+Tab in vb.net RRS feed

  • Question

  • I need to add in the ability to capture the "ALT+TAB" key combo in addition to the Left and Right Win key in the following code snipit. The code works for the two Win keys but I cannot find the correct syntax to add in a test for combo keys like "ALT+TAB".

     Private Function captureKey(ByVal nCode As Integer, ByVal wp As IntPtr, ByVal lp As IntPtr) As IntPtr
            If nCode >= 0 Then
                Dim objKeyInfo As KBDLLHOOKSTRUCT = DirectCast(Marshal.PtrToStructure(lp, GetType(KBDLLHOOKSTRUCT)), KBDLLHOOKSTRUCT)
                If objKeyInfo.key = Keys.RWin OrElse objKeyInfo.key = Keys.LWin Then
                    ' Disabling Windows keys
                    Return CType(1, IntPtr)
                End If
                If objKeyInfo.key = Keys.ControlKey OrElse objKeyInfo.key = Keys.Escape Then
                    ' Disabling Ctrl + Esc keys
                    Return CType(1, IntPtr)
                End If
            End If
            Return CallNextHookEx(ptrHook, nCode, wp, lp)
        End Function

    What do I need to add to the line:

    If objKeyInfo.key = Keys.RWin OrElse objKeyInfo.key = Keys.LWin Then

    To be able capture the "ALT+TAB" combo?

    Thanks to all who trey to help

    Ralph

    • Moved by Oscar_Wu Thursday, October 13, 2016 1:32 AM related to VB
    Wednesday, October 12, 2016 9:43 PM

Answers

  •  If your original keyboard hook code worked,  then you should be able to check for Alt+Tab combo like this.

            If (My.Computer.Keyboard.AltKeyDown AndAlso objKeyInfo.key = Keys.Tab) Then
    
            End If
    


    If you say it can`t be done then i`ll try it

    • Proposed as answer by Mr. Monkeyboy Friday, October 14, 2016 7:20 PM
    • Marked as answer by Ralph Malph 2 Friday, October 14, 2016 7:47 PM
    Friday, October 14, 2016 7:00 PM

All replies

  • Hi Ralph,

    This forum is discussing Visual Studio WPF/SL Designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System, and Visual Studio Editor.

    Your issue may be related to VB development I will move this thread to corresponding forum for a professional answer.

    Sincerely,

    Oscar


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Thursday, October 13, 2016 1:31 AM
  • Hi Ralph Malph 2,

    Here is an example that capture ALT+TABLE keys, please refer that if it helps:

    https://www.daniweb.com/programming/software-development/threads/359374/disable-alt-tab-win-key-ctrl-esc-key

    Best Regards,

    Neda Zhang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 13, 2016 2:51 AM
  • Thanks Oscar for moving this question to the correct thread.

    Maybe one of these days Microsoft will redesign the forums layout and structure so that it is easy to find and so it is easy to locate the correct area to go to ask for help. But at the moment it is so poorly organized that there is no way to be sure where you are posting something when posting a new question from scratch.

    Thanks Neda for trying to help. Unfortunately that code does not work at all. It does absolutely nothing that I can find. I put it in my program, but nothing seems to do anything.

    Maybe I am just not calling the "Jam" and "UnJam" routines correctly. If I created two buttons, "Jam" and "UnJam" what syntax would I put in their click subroutine so that when I clicked "Jam" the "ALT+TAB" etc keys would stop functioning and when I click on the "UnJam" they would start to function again?

    Perhaps...

    call keyboardjammer.Jam() and/or call keyboardjammer.unjam() or what?

    The "Form1_HandleCreated(sender AS Object, e As EventArgs) Handles Me.HandleCreated" runs when I start my program, at least the message box I put in there goes off, but NO keys are blocked at all. The Form1_HandleDestroyed sub routine never seems to go off.

    I know the other method I posted a snipit of works, I just need the syntax for adding in an "Alt+Tab" to my test of the key presssed test IF statement I posted. Do you or does anyone know what that would be?

    If the test for the LWin key is: does "objkeyinfo.key = keys.LWin" then what syntax would I use to test for "Alt+Tab"?

    Thanks again to all those who are trying to help,

    Ralph

    Friday, October 14, 2016 6:24 PM
  • PS I am using VB.Net 2013 on a Win 10 x64 Ent. system.

    Thanks again,

    Ralph

    Friday, October 14, 2016 6:27 PM
  •  If your original keyboard hook code worked,  then you should be able to check for Alt+Tab combo like this.

            If (My.Computer.Keyboard.AltKeyDown AndAlso objKeyInfo.key = Keys.Tab) Then
    
            End If
    


    If you say it can`t be done then i`ll try it

    • Proposed as answer by Mr. Monkeyboy Friday, October 14, 2016 7:20 PM
    • Marked as answer by Ralph Malph 2 Friday, October 14, 2016 7:47 PM
    Friday, October 14, 2016 7:00 PM
  • Thanks IronRazerz!

    Excellent, that is what I needed. It seems to work!

    Ralph

    Friday, October 14, 2016 7:42 PM
  • I was able to use the syntax from IronRazerz and the code from here - minus the timer parts used to stop the TaskMgr which does not work and with a little massaging of it into my code I got it to work as a screen locker. But I still have to disable TaskMgr which I will do via the registry. (I have most of the code worked out, just a few permission things to get through.)


    Thanks again to all who have tried to help,

    Ralph

    Link removed by moderator
    Friday, October 14, 2016 8:48 PM