[VB] Keydown issue?

Discussion in 'Mixed Languages' started by QuantumBug, Nov 16, 2012.

  1. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,545
    1,416
    60
    Since I have a custom frame on my application pressing WinKey + Up maximises the application, so I thought ok I'll add a function that supresses the key press when the WinKey + Up combination is pressed, but oh no, it doesn't work.

    Are either one of these keys not detected as a key press or something? I'm pretty sure the up key isn't.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    #2 stevemk14ebr, Nov 18, 2012
    Last edited by a moderator: Apr 20, 2017
    if your problem is detecting the key press then this will work (make a timer with 100 delay then enable it)
    function:
    Code:
    Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Integer
    Code:
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            If GetAsyncKeyState(Keys.LWin) Then
                MessageBox.Show("winkey pressed")
            End If
            If GetAsyncKeyState(Keys.Up) Then
                MessageBox.Show("up pressed")
            End If
        End Sub
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. master131

    master131 MDL Novice

    Apr 12, 2011
    45
    22
    0
    Can't you just set the MaximizeBox property on your form to False? As far as I know, there's no way to prevent resizing via Windows Key + Up by using the KeyDown event of a form because it only sends the Windows key and not the Up button.
     
  4. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    #4 stevemk14ebr, Nov 18, 2012
    Last edited by a moderator: Apr 20, 2017
    thats why i showed him the getasynckeystate function, it will register all key presses even if the program is not in focus

    EDIT:
    I think i found what you are looking for (found it on a site and modified it a little for your purpose this will disable the winkeyup on form load and reenable it on form close (uses a low-level hook) tested as working (You may have to go to Project / Properties / Debug and uncheck Enable the Visual Studio Hosting process for it to work while running in Visual Studio), here's the class:
    Code:
      Public Class KeyboardJammer
            Private Delegate Function HookCallback(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
            Private Shared HookDelegate As HookCallback
            Private Shared HookId As Integer
            Private Const Wh_Keyboard_LL As Integer = 13
           
            Private Shared Function KeyBoardHookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
                'All keyboard events will be sent here.
                'Don't process just pass along.
                If nCode < 0 Then
                    Return CallNextHookEx(HookId, nCode, wParam, lParam)
                End If
                'Extract the keyboard structure from the lparam
                'This will contain the virtual key and any flags.
                'This is using the my.computer.keyboard to get the
                'flags instead
                Dim KeyboardSruct As KBDLLHOOKSTRUCT = Marshal.PtrToStructure(lParam, GetType(KBDLLHOOKSTRUCT))
                If KeyboardSruct.vkCode = Keys.LWin And Keys.Up Then
                    'Alt Tab
                    Return 1
                End If
                'Send the message along
                Return CallNextHookEx(HookId, nCode, wParam, lParam)
            End Function
            Public Shared Sub Jam()
                'Add the low level keyboard hook
                If HookId = 0 Then
                    HookDelegate = AddressOf KeyBoardHookProc
                    HookId = SetWindowsHookEx(Wh_Keyboard_LL, HookDelegate, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly.GetModules()(0)), 0)
                    If HookId = 0 Then
                        MessageBox.Show("there was an error")
                    End If
                End If
            End Sub
            Public Shared Sub UnJam()
                'Remove the hook
                UnhookWindowsHookEx(HookId)
            End Sub
            <DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
            Private Shared Function CallNextHookEx( _
        ByVal idHook As Integer, _
        ByVal nCode As Integer, _
        ByVal wParam As IntPtr, _
        ByVal lParam As IntPtr) As Integer
            End Function
            <DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall, SetLastError:=True)> _
            Private Shared Function SetWindowsHookEx( _
        ByVal idHook As Integer, _
        ByVal HookProc As HookCallback, _
        ByVal hInstance As IntPtr, _
        ByVal wParam As Integer) As Integer
            End Function
            <DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall, SetLastError:=True)> _
            Private Shared Function UnhookWindowsHookEx(ByVal idHook As Integer) As Integer
            End Function
            Private Structure KBDLLHOOKSTRUCT
                Public vkCode As Integer
                Public scanCode As Integer
                Public flags As Integer
                Public time As Integer
                Public dwExtraInfo As IntPtr
            End Structure
        End Class
    you need these imports:
    Code:
    Imports System.Runtime.InteropServices
    Imports System.Reflection
    to use it just paste these in (do not call them, they automatically are called by form load and close)
    Code:
    Private Sub Form1_HandleCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.HandleCreated
            KeyboardJammer.Jam()
        End Sub
    
        Private Sub Form1_HandleDestroyed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.HandleDestroyed
            KeyboardJammer.UnJam()
        End Sub
    you can add this in a timer with 100 delay to toggle the disabling(e and d):
    Code:
      Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            If GetAsyncKeyState(Keys.E) Then
                KeyboardJammer.Jam()
            End If
            If GetAsyncKeyState(Keys.D) Then
                KeyboardJammer.UnJam()
            End If
        End Sub
    I for one have trouble viewing this in this format so here is the project link here don't use their download manager

    98% of the credit to:http://www.daniweb.com/software-development/vbnet/threads/359374/disable-alttab-win-key-ctrl-esc-key
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,519
    7,112
    120
    It can be solved only setting the MinimumSize and MaximumSize to the same as the Size property?
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    ahah josh i guess you're right, over engineering is fun anyways
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,545
    1,416
    60
    Maximize = False doesn't work

    Thanks I'll try these functions out Ste :D
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    #8 stevemk14ebr, Nov 19, 2012
    Last edited by a moderator: Apr 20, 2017
    during some testing i found that this line
    Code:
    If KeyboardSruct.vkCode = Keys.LWin And Keys.Up Then
                    'Alt Tab
                    Return 1
                End If
    should be broken into 2 such as
    Code:
    If KeyboardSruct.vkCode = Keys.LWin Then
                    'Alt Tab
                    Return 1
                End If
    and
    Code:
    If KeyboardSruct.vkCode = Keys.Up Then
                    'Alt Tab
                    Return 1
                End If
    it seems that it only will jam the first key in an "and" or and "or" statement
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. PAYMYRENT

    PAYMYRENT MDL Developer

    Jul 28, 2009
    1,462
    419
    60
    i wrote a custom keyboard launcher ( adds my own key combos to launch programs and files)

    when the hook fires the event i notices that Shift + L for example came up like ShiftL

    now those hooks work the same way

    If WINU ( shows up like that in the hook) is fired then react to your liking...