Solved

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

Posted on 2006-11-07
3
385 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
0
Comment
Question by:TSFLLC
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
"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...
0
 

Author Comment

by:TSFLLC
Comment Utility
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.
0
 

Author Comment

by:TSFLLC
Comment Utility
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
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now