Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   C# Key Checking (http://www.go4expert.com/forums/c-sharp-key-checking-t713/)

TinyClone 12Apr2006 23:02

C# Key Checking
 
Because there no forum for C# i'll post i here:

I want to check whether a specific key is press a the moment.
However, I can only find functions to check this on events.
I want to check this when there's no event called.

shabbir 13Apr2006 06:40

Re: C# Key Checking
 
There is a forum for C# and I have moved the post to the correct section.

coderzone 13Apr2006 06:42

Re: C# Key Checking
 
When you are not in a key press event that does mean key is not pressed and when ever its pressed the event will be fired.

TinyClone 14Apr2006 18:22

Re: C# Key Checking
 
no its not actually. It fires only the first time.

EDIT: I solved it now for 4 keys with an array which remembers the states.

coderzone 14Apr2006 18:56

Re: C# Key Checking
 
Quote:

Originally Posted by TinyClone
no its not actually. It fires only the first time.

EDIT: I solved it now for 4 keys with an array which remembers the states.

Can you explain a bit more on what you mean fires for the first time. Do you mean it fires once for multiple key fire events.

TinyClone 15Apr2006 21:31

Re: C# Key Checking
 
it only fires when you press it down, not when it is pressed:
When I keep the down key pressed it fires only once; I want to check if its down every 1/40 second.
I did it though on the way described one post earlier.

shabbir 17Apr2006 10:49

Re: C# Key Checking
 
What event you are catching. KeyPress or KeyDown.

TinyClone 17Apr2006 17:18

Re: C# Key Checking
 
doesnt matter; i want to let it fire like 40 times a sec, and KeyPress only fires on press after its released; KeyDown only fires on pressing the key down (1 time).

SCPP 26May2009 05:32

Re: C# Key Checking
 
Quote:

Originally Posted by TinyClone (Post 2215)
doesnt matter; i want to let it fire like 40 times a sec, and KeyPress only fires on press after its released; KeyDown only fires on pressing the key down (1 time).

Well you could use the event approach and create a variable to store the state of your key. Detect when the keydown event indicates it has been pressed.

E.g.

Code:

private bool ControlKeyDown = false;

private void txtSendData_KeyDown(object sender, KeyEventArgs e)
{
    if (KeyHandled = e.KeyCode == Keys.ControlKey)
    {   
        ControlKeyDown = true;
    }
}

private void txtSendBox_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.ControlKey)
    {
        ControlKeyDown = false;
    }
}

Then all you'd have to do is check your ControlKeyDown variable. It will get updated in realtime by the event handlers.

night.rider 18Aug2009 20:56

Re: C# Key Checking
 
you cannot check the key char until tthe key is pressed and when any key is pressed a keyevent is fired..
and this depends on you that which event u want to fire either event on pressing or after pressing or after key is up and so on..

so u only need is in event is the args as (object sender, KeyEventArgs e)
now u have different properties with 'e' and using intellisence check whether there is keychar prop so u write as follows..
if(e.keychar<64 && e.keychar>91) to check if Capitol alphabet is pressed(i.e caps +alphabet) and so on..
e.keychar returns ascii values of key pressed

Check and do tel me how it runs.. m waiting


All times are GMT +5.5. The time now is 07:56.