Limiting the Acceptance of Keyboard Interrupts
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23

Thread: Limiting the Acceptance of Keyboard Interrupts

  1. #1
    Join Date
    Apr 2013
    Posts
    9

    Limiting the Acceptance of Keyboard Interrupts

    Hey Codeguru community, I need some quick advice on a problem and just found out that this site existed, hope I can find some help here.

    I'm designing a program that will accept commands via the keyboard, I've already programmed the interrupts so that when they're pressed an action will occur depending on what key was pressed, but I want it to only interrupt the program after a certain button on the main form was clicked, my issue is that if I haven't clicked the button yet and keys are being quickly pressed one after another, the program will become constantly interrupted and will essentially freeze the form from accepting any clicking on any buttons or what-not.

    My question is, is there a way to have the program accept interrupts after a certain event has passed, aka. I've clicked the button on the main form. I'm not sure if there's some sort of enable_interrupt variable or something that's intrinsically included in a VB program.

    Any help would be appreciated, thank you.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,101

    Re: Limiting the Acceptance of Keyboard Interrupts

    Maybe a stupid suggestion from an old burnt-out programmer, but have you tried DoEvents?

  3. #3
    Join Date
    Apr 2013
    Posts
    9

    Re: Limiting the Acceptance of Keyboard Interrupts

    I'm not sure what that is, I'll look into it. Thanks for the suggestion.

  4. #4
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,862

    Re: Limiting the Acceptance of Keyboard Interrupts

    ummm sounds like you just need to add a flag in your code. In your code that processes the keys just test the flag and if not true then don;t do anything. In your button click set the flag to true.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Apr 2013
    Posts
    9

    Re: Limiting the Acceptance of Keyboard Interrupts

    I do have a flag variable so that it only processes what I want after the button is clicked, but the issue is that every time a keyboard press is done, it calls the interrupt function regardless and then checks the flag, that check if done repeatedly essrntially freezes the main form ans that's what I'm trying to fix.

  6. #6
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,862

    Re: Limiting the Acceptance of Keyboard Interrupts

    I think you should show some code, an event such as keypress where the only thing there is a test of a flag var should not interfere with the program in any noticeable way also if the user is typing that fast they probably would not be clicking on anything anyway.

    Then again I am not sure what you are really trying to say, you use the term keyboard interrupts but I think that maybe you mean something other than what that term implies.
    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    15,002

    Re: Limiting the Acceptance of Keyboard Interrupts

    sure, like a keylogger?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Apr 2013
    Posts
    9

    Re: Limiting the Acceptance of Keyboard Interrupts

    No not a keylogger, I'll post some code to make it clearer next time I'm at the computer with the project saved.

  9. #9
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Limiting the Acceptance of Keyboard Interrupts

    If you're not already using the keyboard events of the form, then that would be the first thing to do. Then in the appropriate event, test the flag, and exit the sub if it is false.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  10. #10
    Join Date
    Apr 2013
    Posts
    9

    Re: Limiting the Acceptance of Keyboard Interrupts

    Here's the event handling for a keypress, this is just part of it but if the keyboard is getting mashed on before "readyFlag" is true then it keeps checking this handler to check that variable even if it isn't set yet, and that constant rechecking basically freezes up the main form since the program keeps changing focus to this handler instead of the main code.

    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
    Is there a way to not have the program get to this handler code before the readyFlag is set to true? Even if keys are being pressed?

  11. #11
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,862

    Re: Limiting the Acceptance of Keyboard Interrupts

    Well... first of all this is not VB6 code but VB.Net so I am moving this to the VB.Net section.

    If your readyflag var is defined properly then it will have a starting value of False before any event handler fires
    Your readyflag test should be the outter test rather than the inner test, meaning you should test that first.

    That said that code you posted would not do much and surely would not hang your program at all.

    No you can not prevent the event from firing when a key is pressed unless of course you remove the event from your code.

    Also note that your thread is mis titled as this has nothing to do with keyboard interrupts.
    Always use [code][/code] tags when posting code.

  12. #12
    Join Date
    Jun 2004
    Location
    NH
    Posts
    674

    Re: Limiting the Acceptance of Keyboard Interrupts

    Code:
    Sub MainScreen_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles Me.KeyPress
    
            If readyFlag = False Then exit sub
    
            If e.KeyChar = ChrW(122) Then
               color1.BackColor = Color.Green
               readyFlag = False
               lblGO.Visible = False
            End If
    End Sub
    That should help.

  13. #13
    Join Date
    Apr 2013
    Posts
    9

    Re: Limiting the Acceptance of Keyboard Interrupts

    Thanks for the advice TT(n), unfortunately with that code I still run into the same problem, the program keeps changing focus to the keyhandler so if the keys are getting pressed rapidly in succession the main form still hangs and stops accepting button clicks lest I spam my clicks faster than the keyboard inputs.\

    I'm just going to assume it can't be done the way I want, I was just hoping there was some global variable that would enable or disable keyboard inputs to the program, that I could change when I want the program to start accepting keyboard inputs.

  14. #14
    Join Date
    Jun 2004
    Location
    NH
    Posts
    674

    Re: Limiting the Acceptance of Keyboard Interrupts

    Don't be frustrated just yet. We just didn't think the handler itself would cause a hang. I could see the overhead of calling ChrW for each repeat but not the boolen flag check. You have alternatives though. Although I wonder why your computer would hang unless there is a keyboard driver issue underlying. I've never seen it happen. If it really is caused by keys repeating at a rate that causes the handler to hang then try it only when the key is down or up:


    Code:
        Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            If e.KeyCode = Keys.A Then
    
            End If
        End Sub
    
        Private Sub Form1_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
            If e.KeyCode = Keys.B Then
    
            End If
        End Sub
    That will avoid what is called re-entrance.
    And ofcourse this may be helpful as well to toggle the state of the handler:
    Code:
    Me.KeyPreview = True
    Last edited by TT(n); May 9th, 2013 at 09:36 PM.

  15. #15
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    15,002

    Re: Limiting the Acceptance of Keyboard Interrupts

    or, a bad keyboard itself. try on another...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Page 1 of 2 12 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center