How can I find out which key was pressed
-
I have a textbox on my form. Now, I want to find which key was pressed in the textbox. I mean i want to find which key pressed including Function keys F1, F2, etc...Modifier Keys like SHIFT, CTRL, and ALT. How can I do this.
-
I have a textbox on my form. Now, I want to find which key was pressed in the textbox. I mean i want to find which key pressed including Function keys F1, F2, etc...Modifier Keys like SHIFT, CTRL, and ALT. How can I do this.
I looked up on Google and I found a post on a forum that said taht you have to use the System.Threading.Thread.Sleep(int milliseconds). Howecer, when i do this the UI hangs. Basically, what I'm trying to accomplish here goes like this: I have a for loop and between loop iterations i wish to put a 5 second delay. Now what?
-
I have a textbox on my form. Now, I want to find which key was pressed in the textbox. I mean i want to find which key pressed including Function keys F1, F2, etc...Modifier Keys like SHIFT, CTRL, and ALT. How can I do this.
If you catch the KeyPressed event, you get a different event args to KeyDown/KeyUp. There's another event for control keys as well, I think. You can also call GetAsyncKeyState via pinvoke, for full control over what control keys are down, etc, during any keyboard event, or anywhere in your code, for that matter. KeyDown/KeyUp have the properties to say if a control key was pressed, KeyPressed actually gets a different key code ( in the range 0-25, I *think* ) if Control is also down. Christian Graus - Microsoft MVP - C++
-
I looked up on Google and I found a post on a forum that said taht you have to use the System.Threading.Thread.Sleep(int milliseconds). Howecer, when i do this the UI hangs. Basically, what I'm trying to accomplish here goes like this: I have a for loop and between loop iterations i wish to put a 5 second delay. Now what?
Mridang Agarwal wrote: looked up on Google and I found a post on a forum that said taht you have to use the System.Threading.Thread.Sleep(int milliseconds). Bizarre. This is a pile of bull. Why would you have to sleep to get a key event ? But good for you for trying to find the answer all the same. Mridang Agarwal wrote: I have a for loop and between loop iterations i wish to put a 5 second delay. Now what? Oh, this is a different question ? Yes, Thread.Sleep is your answer, but it stops your app running, obviously. The other possibility is to put this code in a thread, so only that thread sleeps, or create a big old ugly loop that uses datetime.now over and over, and calls Application.DoEvents. Why do you want your app to sleep for 5 seconds ? Christian Graus - Microsoft MVP - C++
-
Mridang Agarwal wrote: looked up on Google and I found a post on a forum that said taht you have to use the System.Threading.Thread.Sleep(int milliseconds). Bizarre. This is a pile of bull. Why would you have to sleep to get a key event ? But good for you for trying to find the answer all the same. Mridang Agarwal wrote: I have a for loop and between loop iterations i wish to put a 5 second delay. Now what? Oh, this is a different question ? Yes, Thread.Sleep is your answer, but it stops your app running, obviously. The other possibility is to put this code in a thread, so only that thread sleeps, or create a big old ugly loop that uses datetime.now over and over, and calls Application.DoEvents. Why do you want your app to sleep for 5 seconds ? Christian Graus - Microsoft MVP - C++
Oh !#$#. What he hell did I just post? That was a big goof up. I was posting a reply to another post and I accidentally posted it here. Sorry!
-
I have a textbox on my form. Now, I want to find which key was pressed in the textbox. I mean i want to find which key pressed including Function keys F1, F2, etc...Modifier Keys like SHIFT, CTRL, and ALT. How can I do this.
You'll simply need to handle the "KeyDown" event of the TextBox like so: Just put a similar code in the KeyDown event of the TextBox:
If e.KeyCode = Keys.Enter Then 'Whatever you wanna do End If
The keys F1, F2, etc... are also present in the Keys enumeration (Keys.F1, Keys.Tab, Keys.a, etc...) PEACE! -
I have a textbox on my form. Now, I want to find which key was pressed in the textbox. I mean i want to find which key pressed including Function keys F1, F2, etc...Modifier Keys like SHIFT, CTRL, and ALT. How can I do this.
how are you my friend . listen my friend about handling the keydown event is not useful and wasting time if you try to handle it through your keydown event handling cause you will need to nest alot alot of if statements and switch keywords and it is not practical you need something like keylogger code to log any keys pressed even if your form is activated or not then you can handle the specific key when it is pressed like the following code :-
/*you need first to interoperate dll found in the system itself so you need to call the namespace for handling this */ system.Runtime.InteroServices; /* Then you need to call the function of keys and override it */ [Dllimport("User32.dll")] Public static extern short GetAsyncKeyState(System.Windows.forms.Keys vkey); [Dllimport("User32.dll")] public static extern short GetAsyncKeyState(System.Int32 vkeys); /* now we called the User32.dll found in your system32 of your current windows Os and called the function that handling the keys pressed from the keyboard to get their values and iterate through system Asynchronously */ //You need to initialize timer to Listen to keys pressing //As the following System.Timers.Timer mytimer; //and initialize new keybuffer to store the keyvalue in it System.String KeyBuffer; //Initialize the event handler model of the timer to listen //to the keypressing as the following Private void mytimer_Elapsed(object o , System.Timers.ElapsedEventargs args) { foreach(System.Int32 i in Enum.GetValues(typeof(Keys))) { if(GetAsyncKeyState(i) == -32767) { //here we store the key Value in the keybuffer string //we initialized At the begining. keybuffer += Enum.GetName(typeof(keys),i); TextBox.Text += KeyBuffer; Keybuffer = ""; } } } //in the constructor add the delegate of this event public YourKeyLogger() { mytimer.Elapsed += new ElapsedEventHandler(this.mytimer_elapsed); }
I hope the code can do the work for you Thanks my friend Miss With The Best And Die Like The Rest -
If you catch the KeyPressed event, you get a different event args to KeyDown/KeyUp. There's another event for control keys as well, I think. You can also call GetAsyncKeyState via pinvoke, for full control over what control keys are down, etc, during any keyboard event, or anywhere in your code, for that matter. KeyDown/KeyUp have the properties to say if a control key was pressed, KeyPressed actually gets a different key code ( in the range 0-25, I *think* ) if Control is also down. Christian Graus - Microsoft MVP - C++
i got the solution finally. I put the in the textbox_KeyUp event and wrote e.Keydata.ToString();