-
April 22nd, 2013, 05:38 AM
#1
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.
-
April 22nd, 2013, 05:46 AM
#2
Re: Limiting the Acceptance of Keyboard Interrupts
Maybe a stupid suggestion from an old burnt-out programmer, but have you tried DoEvents?
-
April 23rd, 2013, 06:24 PM
#3
Re: Limiting the Acceptance of Keyboard Interrupts
I'm not sure what that is, I'll look into it. Thanks for the suggestion.
-
April 24th, 2013, 04:11 PM
#4
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.
-
April 24th, 2013, 07:21 PM
#5
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.
-
April 24th, 2013, 07:29 PM
#6
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.
-
April 24th, 2013, 10:11 PM
#7
Re: Limiting the Acceptance of Keyboard Interrupts
sure, like a keylogger?
-
April 25th, 2013, 07:58 AM
#8
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.
-
April 25th, 2013, 03:18 PM
#9
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?
-
April 27th, 2013, 02:09 AM
#10
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?
-
April 27th, 2013, 09:42 AM
#11
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.
-
May 3rd, 2013, 07:03 PM
#12
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.
-
May 9th, 2013, 08:04 PM
#13
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.
-
May 9th, 2013, 08:33 PM
#14
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 08:36 PM.
-
May 9th, 2013, 11:02 PM
#15
Re: Limiting the Acceptance of Keyboard Interrupts
or, a bad keyboard itself. try on another...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|