Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-11-07
3
Medium Priority
?
398 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

730 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