I'm making a speed game in VB.Net (Console Application) and users have a certain time to press a certain key. So how can I give the Console.ReadKey function a timeout without just waiting until a key was pressed?
You should use a timer. When the user selects the correct key you can disable it. If the user is to then choose a different key you can just reset it.
Related
I have a long running application written in VBA. Because it runs for a long time I call a sub from the main loop every iteration to check if the user has pressed the escape key. The sub is listed below. The code works great except that it is always listening, even when the VBA application does not have the focus. What's the best way to approach this? Is there an alternative to GetAsyncKeyState which only listens when the correct application is in focus, or are there system calls I can use to check that the correct window is in focus.
Private Sub checkForUserEscKeyAbort()
'Listen for escape key and exit gracefully if user aborts.
Dim abortResult As VbMsgBoxResult
If GetAsyncKeyState(vbKeyEscape) Then
abortResult = MsgBox("Escape key pressed, do you want to abort?", vbYesNo)
If abortResult = vbYes Then Call teardownSLICER
End If
End Sub
The problem is the way GetAsyncKeyState operates. It does more than just check it the key is currently down, it also checks if the key was pressed since the last time GetAsyncKeyState was called. Thus, your problem of "always listening".
You could use GetKeyState, but frankly, I'm not a fan of that option either. Your code must be well crafted to avoid a polling window that is so small that you literally have to hold the key down to ensure it isn't missed.
A viable alternative is key masking where you use a combination of keys such as Shift-Escape. There is a decent introduction to this (and many other subjects) on Chip Pearson's site. However, this is still not my preferred method.
My preference has already mentioned in the comments. Your application may best be improved with a user form. It also gives you the ability to get information in front of the user. Perhaps they wouldn't try quitting the application if a progress bar on the user form indicated 95% completion. Maybe you can add a pause button that free's some resources for a prescient need and then resumes when the user is ready. That little extra functionality on its own is enough to win me over but there is an even better reason for using a User Form - it does exactly what you asked!
User forms, and many user form controls, have Keydown, Keyup, and Keypress events that only trigger when the form (or control) have focus. This is precisely what you wanted.
Im not sure if this is possible but I am working on a small project where I would like to be able to enter in a specific sequence of keys and then something happens. Its purpose is to act as a sort of "password" to unlock additional functionality. Im thinking maybe I could store key presses in an array somehow and compare the array to another that has the same key presses in it already or something similar but im not sure on where to start.
This is an auto talker that i created, but it isn't very convenient to click on start and stop when the text is being typed at 10 milliseconds. So i wanted to create a key that when pressed starts the auto typer, and also stops it. I have a box where the user can press a key to assign to start and stop the auto typing. What code would i put for clicking on assign? I want assign to: get key from textbox5.text and use it as the shortcut key to start spamming. The key would be pressed when the Interface for the spam bot is minimized, so i can't use "&Spambot" for S to be the shortcut key.
Any help would be appreciated.
Please click on the link below to see image
http://s16.postimg.org/6xdxtjvg5/Interface.png
I successfully used some methods to read the numkey state, started my stuff when it is turned on, and stop it when it is turned off again, maybe you will like that solution.
If Control.IsKeyLocked(Keys.NumLock) Then ...
your alternative would be to 1. use an keyDown event (i think it gave more information about pressed keys than key pressed event), then store it in a local variable
And finally using an external dll to figure out if any key is pressed anywhere in windows. And if, then compare if it is the correct key, and then trigger whatever you want to trigger.
How it could be done?
So far i seen on Google:
Virtual keyboard
Add some events to my textbox to cancel the key event.
When key pressed record what has been pressed save and then simulate other 10 random keys?
what else can be done and not to build the whole anti-keylogger antivirus.
It can be done anything that your imagination let you:
You could tell the user to put his/her password using the key in the direction you want, for example on the right. And control the symbols with your application (with a Dictionary for example). You could show a standart keyboard picture to the user to be sure.
You could tell the user to put a letter you show to him/her every X keys pressed, or just press any key he/she wants and discard it.
You could tell the user to put a letter you show to him/her after each key of password pressed.
You could tell the user to put only the even characters of his password, and later ask for the rest.
You could tell the user to put the password backwards.
But I think a Virtual Keyboard is the best option.
I wonder is there a way, to handle key event if my application not in focus.
For example, I create a small app. It has a transaparent background. When I run my app via Xcode(run option), it is in focus and handle key events. But if I click anywhere, focus lost and key events doesn't handled. All I need is handle key event even if my application not in focus(with transaparent background), and if send key events to other windows(in focus), if this key events won't needed for my application. Dropl is application, that doing just what I want. But I don't have the source.
Ok, I solwed my problem using DDHotKeyExample from https://github.com/davedelong/DDHotKey