CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 23 of 23
  1. #16
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Limiting the Acceptance of Keyboard Interrupts

    As others have stated, there shouldn't be an issue from repeated key presses. I tested with the attached form, and it works fine even if a key is held down. It does not prevent the button from being clicked.

    I wonder if FilterKeys is on, and if that might be interfering. Check your keyboard settings for that.

    Perhaps if you could explain why there would be rapid-fire keyboard events, it might shed some light on the issue.
    Attached Files Attached Files
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  2. #17
    Join Date
    Apr 2013
    Posts
    9

    Re: Limiting the Acceptance of Keyboard Interrupts

    Quote Originally Posted by WizBang View Post
    As others have stated, there shouldn't be an issue from repeated key presses. I tested with the attached form, and it works fine even if a key is held down. It does not prevent the button from being clicked.

    I wonder if FilterKeys is on, and if that might be interfering. Check your keyboard settings for that.

    Perhaps if you could explain why there would be rapid-fire keyboard events, it might shed some light on the issue.
    Would holding down a button repeat the keyhandler function? I suppose it would

    Filter Keys is not on, but that was a good guess.

    I made the program as sort of a trivia game, I have several controllers attached and sending input via Joy2Key as a workaround of me not knowing how to program direct input from a game controller, thus when a button is pressed on the controller it sends a keyboard input to the program. Essentially I click a button when it's time for the contestants to enter their answer, but if they're already mashing the button beforehand (all 4 of them in worst instances) it essentially hangs my program so when I try and click the button on the form it doesn't register my click, until I click fast enough that I get one in in-between all the button mashing.

    The ButtonDown and Up separate functions seem like a good workaround, it'd probably cut the load in half, I'll try that.

  3. #18
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Limiting the Acceptance of Keyboard Interrupts

    I think that the keypressed event does not fire until the key is released so no it should not continue to fire if the key is held down.
    Always use [code][/code] tags when posting code.

  4. #19
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Limiting the Acceptance of Keyboard Interrupts

    Ah! That's probably the problem - Joy2Key. Perhaps if you only activate the Joy2Key utility when you click the button, and deactivate it when the first valid signal is received from one of the controllers, then there might not be a hanging issue. You might also play a sound whenever a player tries to press a button before it should be accepted. I also wonder if calling DoEvents first, within the keyboard event, might allow your mouse click to get through. However, I'd still recommend learning to use the API functions associated with joystick input signals. Then you'd not have to deal with such external software.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  5. #20
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: Limiting the Acceptance of Keyboard Interrupts

    Ok that is why the problem was not easy to diagnose. You are using Joy2key and not the physical keyboard. Now it makes sense why you posted the subject as interrupts.

    Have you tried it with the handler fully commented out?
    Just make sure it's not on the joy2key side.

    EDIT: Is that the only code you have in the key event?
    If you have any code that takes a long time to execute that has not been posted then you will need to use a thread or backgroundworker.
    Last edited by TT(n); May 13th, 2013 at 10:31 AM.

  6. #21
    Join Date
    Apr 2013
    Posts
    9

    Re: Limiting the Acceptance of Keyboard Interrupts

    Quote Originally Posted by Chumara View Post
    Code:
    Sub MainScreen_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles Me.KeyPress
            If e.KeyChar = ChrW(122) Then
                If readyFlag = True Then
                    color1.BackColor = Color.Green
                    readyFlag = False
                    lblGO.Visible = False
                End If
            End If
    End Sub
    I have this code repeated 5 times, checking for different ChrW values. I wish I knew more about programming and I admit I have no idea how to program in joystick inputs, but I feel stupid for repeating that readyflag check 5 times, I think just pulling that out of my If statements will lessen the problem a bit. It's really hard for me to test these ideas because I can't emulate 5 people mashing 5 buttons in rapid succession by myself, I can't even recreate my current issue by myself, it's only while using it in it's practical use that I run into this problem. I appreciate the help, but I think I'll just deal with it for now and hope that pulling the readyflag out of the If statements solves the problem a bit.
    What I had hope existed in VB I guess doesn't exist after all.

    EDIT: Nevermind, apparently if I just use my keyboard to hit all 5 keys in succession it emulates the problem, I was trying to hold them down like WizBang had said and hadn't run into trouble. Pulling the readyflag check out is better in terms of principal, but still didn't get rid of the issue, also I was doing this without Joy2Key so I guess I can't blame that. I was really hoping there was some way of turning off and on the keyhandler. Maybe if I embed the keyhandler inside an infinite do while loop?
    Last edited by Chumara; May 15th, 2013 at 06:46 PM.

  7. #22
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: Limiting the Acceptance of Keyboard Interrupts

    You are doing the ChrW function over and over. Your boolean readyflag check should be before that. This boolean check is ok, it doesn't cost much. That data type is the least expensive to check.
    Did you try it with the keydown handler?


    I was really hoping there was some way of turning off and on the keyhandler.
    There is.
    Here is one more example of how to turn the handler off just like you want. I posted this before.

    Code:
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Me.KeyPreview = True
        End Sub
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Me.KeyPreview = False
        End Sub
    
        Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            MsgBox("pressed " & e.KeyCode.ToString)
        End Sub

  8. #23
    Join Date
    Apr 2013
    Posts
    9

    Re: Limiting the Acceptance of Keyboard Interrupts

    Oh cool, sorry I missed that. So I can put Me.KeyPreview.False in the main form so it initializes that way, then set it to True when I click the button to start accepting inputs, and then set it back to False after the ChrW/button checks? I'll try it right now, this might have been the simple answer I was looking for.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured