We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

SendKey.Keys("{UP}") is causing my application to lock up

TSFLLC
TSFLLC asked
on
Medium Priority
425 Views
Last Modified: 2012-06-21
Below is a piece of code that I modified from some code I found on EE created by Ronald Biemans.  It deals with capturing the UP ARROW key and based on whether a listbox is visible, I send the key or I don't.  The DON'T SEND was the main thing I was looking for and it works like a charm.

My problem now is that if I use the SendKeys with UP ARROW it locks up VB.NET.  If I send a standard character it works fine.

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If msg.WParam.ToInt32() = CInt(Keys.Up) Then
            If lbAutoComplete.Visible = True Then
                ' DO NOTHING, IGNORE UP ARROW
            Else
                SendKeys.Send("{UP}")
            End If
            Return True
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function 'ProcessCmdKey


I have also tested the code below that he included also if SendKeys won't work.  At least with this my application doesn't lock up, but I have several ideosync.... when I attempt to use it.

1)  The UP ARROW does not fire.
2)  Any other buttons on the form depress but don't work.
3)  The cursor quit blinking.

    Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, _
            ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)

    Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" _
        (ByVal wCode As Integer, ByVal wMapType As Integer) As Integer

    Private Sub UpArrow()
                keybd_event(Keys.Up, MapVirtualKey(Keys.Up, 0), 0, 0)  ' Down ARROW UP
                keybd_event(Keys.Up, MapVirtualKey(Keys.Up, 0), 2, 0) ' Up ARROW UP
    End Sub


I don't fully understand all of the aspects of the Override code above, but it gave me the action I was looking for in that it ignores the UP ARROW keypress.  Please help because it's critical I resolve this so that I can incorporate it into a bunch of forms and complete this project.

Sincerely,
Phil Tate
Comment
Watch Question

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
"My problem now is that if I use the SendKeys with UP ARROW it locks up VB.NET."

Well...yeah!  You've created an infinite loop!

Your logic says:

   If Up Arrow is Pressed and lbAutoComplete.Visible = False Then Send Another Up Arrow

This causes the same code to run over and over and over and over and over...

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Ok Mike....I see it now like a chess move but a little too late.  The original code was checking for one key but it was sending another.

Do you see any possible way to capture whether a key has been pressed and ignore it in once instance and not ignore it in another?

I have an autocomplete textbox that pops up a listbox under it.  The up arrow scrolls through the listbox, but it changes the position of the cursor needless to say each time I press the up arrow.  In MS Outlook the cursor does not move in the text box.  It sits to the right of the text value and blinks nice and pretty and doesn't give me grey hair.

In my autocomplete, everything and I mean everything works the way I want it to except for the fact that the cursor moves left...then right at the end of the text value based on my Keydown, KeyUp events.  So the first part of the code above resolved that problem but it has created another.

Anything you can recommend?

Thanks.

Author

Commented:
This modification and addition solved the problem.

Private glUpKey as boolean = True

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If glUpKey Then
            If msg.WParam.ToInt32() = CInt(Keys.Up) Then
                If lbAutoComplete.Visible = True Then
                Else
                    SendKeys.Send("{UP}")
                    keybd_event(Keys.Up, MapVirtualKey(Keys.Up, 0), 2, 0) ' Down Arrow Up
                    glUpKey = False
                End If
                Return True
            End If
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function 'ProcessCmdKey

    Private Sub txtAutoComplete_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Handles Me.KeyUp
        Activating = True
        If lbAutoComplete.Visible = True Then
            ' Change selectedrow of listbox
        End If
        If e.KeyCode = Keys.Up Then
            glUpKey = True  'Reset Arrow Up flag
        End If
        Activating = False
    End Sub


I'll give you the points Mike because no telling how long it might have taken me before realizing it was in a loop.

Phil
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.