Solved

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

Posted on 2006-11-07
3
391 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

821 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