Solved

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

Posted on 2006-11-07
3
386 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
ID: 17892942
"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
ID: 17893124
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
ID: 17893391
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
A short film showing how OnPage and Connectwise integration works.

939 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