Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

keyboard input

This topic is 2404 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to get input from the keyboard. I get an error with the e.Key command.
here is some code.


[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]private[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]void[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] Form1_KeyDown([/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]object[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] sender, [/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]KeyEventArgs[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] e)[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/font][/font]


[font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Graphics[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] g = [/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]this[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"].CreateGraphics();[/font][/font]


[font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]SolidBrush[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] whiteBrush = [/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]new[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]SolidBrush[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]([/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Color[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"].White);[/font][/font]


[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]switch[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] (e.Key)[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/font][/font]


[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]case[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] Key.Enter:[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/font][/font]


[font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Rectangle[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] rect_nine = [/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]new[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Rectangle[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"](337 + i, 500, 100, 20);[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]g.FillRectangle(whiteBrush, rect_nine);[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]i--;[/font][/font]


[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]break[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"];[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/font][/font]


[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]default[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]: [/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]break[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"];[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/font][/font]


[font="Consolas"][size="2"][color="#008000"][font="Consolas"][size="2"][color="#008000"][font="Consolas"][size="2"][color="#008000"]// Rectangle rect_ten = new Rectangle(350 + i, 300, 15, 15);[/font][/font][/font]


[font="Consolas"][size="2"][color="#008000"][font="Consolas"][size="2"][color="#008000"][font="Consolas"][size="2"][color="#008000"]// g.FillRectangle(whiteBrush, rect_ten);[/font][/font][/font]


[font="Consolas"][size="2"][color="#008000"][font="Consolas"][size="2"][color="#008000"][font="Consolas"][size="2"][color="#008000"]// i++;[/font][/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]g.Dispose();[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/font][/font]

Share this post


Link to post
Share on other sites
Advertisement
You should really know by now that when you make a post about an error you are getting that you should include the actual error. In any case, the problem is probably because System.Windows.Forms.KeyEventArgs doesn't have a Keys member. It has a KeyCode member, a KeyData member and a KeyValue member.

Share this post


Link to post
Share on other sites
well I have tried several different things from google sites about keyboard input but for some reason I cant get c# to recognize the input from the keyboard.
here is some code


[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]private[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]void[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] Form1_KeyPress([/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]object[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] sender, [/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]KeyPressEventArgs[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] e)[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/font][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] (e.KeyChar == [/font][/font][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"]'a'[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"])[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/font][/font]
[font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Application[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"].Exit();[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]?[/font][/font]

Share this post


Link to post
Share on other sites
Two obvious things spring to mind:

1) Ensure the form named "Form1" has focus. No focus = no keyboard events
2) Ensure caps lock is off and shift or control are not held down or stuck. e.KeyChar returns the ascii character of the key pressed, and will be affected by ctrl, shift or caps lock. Your test above is purely looking for a lower case a.

If you put a breakpoint in the KeyPress event does it fire at all?

Share this post


Link to post
Share on other sites
I have tried every thing I can think of.I just cant get this snippet of code to work.


[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]private[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]void[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] Form1_KeyPress([/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]object[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] sender, [/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]KeyPressEventArgs[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] e)[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/font][/font]


[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] (e.KeyChar == [/font][/font][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"]'a'[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"])[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/font][/font]


[font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Application[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"].Exit();[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]e.Handled = [/font][/font]

[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]true[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"];[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/font][/font]

Share this post


Link to post
Share on other sites
A likely solution, I use this for a game with a window and input box


// keyboard press over ride
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (TextInputBox.Focused)
{
// i want users to be able to type in letters here so i exit if this box is focused.
return false;
}

if (keyData == Keys.Left || keyData == Keys.A)
{
Move("west");
return true;
}
else if (keyData == Keys.Right || keyData == Keys.D)
{
Move("east");
return true;
}
else if (keyData == Keys.Up || keyData == Keys.W)
{
Move("north");
return true;
}
else if (keyData == Keys.Down || keyData == Keys.X)
{
Move("south");
return true;
}
else
return base.ProcessCmdKey(ref msg, keyData);

}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!