简体   繁体   中英

How does one make an object move with arrow keys?

I want to use a shape of some sort for now, but would like to replace it later on with a graphic, which I think is easy enough. But how would I use the arrow keys to control this and move it side to side?

You can use event handlers - something like this:

private void Form_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{ 
   if (e.KeyCode == Keys.NumPad0)
     {
       MyShape.Width ++ ;
     }
}

See here: for more examples: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown.aspx

You might use the event handlers of KeyUp and/or KeyDown , or possibly you might need PreviewKeyUp and/or PreviewKeyDown instead.

In the handlers you can then update the position of your shape/ graphic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM