Detecting arrow key input (keypresses) - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Detecting arrow key input (keypresses)

ITAITA Posts: 463Member
I am making a simple game in which the user is able to move a simple icon(bitmap) with the arrow keys. How do I detect keypresses from the arrows? Also how do I move the bitmap to a new co-ordinate? Please help. Many thanks.

ITA :-)

Comments

  • pingpongpingpong Posts: 937Member
    Here you go, press the arraw keys to move around, +/- on the keypad to increase/decrease speed:

    1. start with a new windows application.
    2. add the following private variables:
    [code]
    private Image _image; // the image
    private Point _pos; // current image position
    private int _speed; // current speed
    [/code]
    3. in your Form1 constructor, after the TODO comment, add the following:
    [code]
    // use double buffer to minimize flickering.
    SetStyle(ControlStyles.ResizeRedraw, true);
    SetStyle(ControlStyles.AllPaintingInWmPaint, true);
    SetStyle(ControlStyles.UserPaint, true);
    SetStyle(ControlStyles.DoubleBuffer, true);
    [/code]
    4. Form1_Load like this:
    [code]
    // load the image
    _image = Image.FromFile(@x:somethingsomething.bmp);
    // start at 0,0
    _pos = Point.Empty;
    // seed of 1
    _speed = 1;
    Text = "Speed: = " + _speed;
    [/code]
    5. Form1_KeyDown like this:
    [code]
    private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
    switch(e.KeyCode)
    {
    case Keys.Left:
    UpdatePosition(-_speed, 0);
    break;

    case Keys.Right:
    UpdatePosition(_speed, 0);
    break;

    case Keys.Up:
    UpdatePosition(0, -_speed);
    break;

    case Keys.Down:
    UpdatePosition(0, _speed);
    break;

    case Keys.Add:
    _speed++;
    _speed = Math.Min(10, _speed);
    Text = "Speed: = " + _speed;
    break;

    case Keys.Subtract:
    _speed--;
    _speed = Math.Max(1, _speed);
    Text = "Speed: = " + _speed;
    break;

    default:
    return;
    }
    }

    // the move helper function
    private void UpdatePosition(int dx, int dy)
    {
    Point newPos = new Point(_pos.X + dx, _pos.Y + dy);

    // dont go out of window boundary
    newPos.X = Math.Max(0, Math.Min(ClientSize.Width - _image.Width, newPos.X));
    newPos.Y = Math.Max(0, Math.Min(ClientSize.Height - _image.Height, newPos.Y));

    if(newPos != _pos)
    {
    _pos = newPos;
    Invalidate();
    }
    }
    [/code]
    6. Finally your paint code:
    [code]
    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
    e.Graphics.DrawImage(_image, _pos.X, _pos.Y, _image.Width, _image.Height);
    }
    [/code]
  • ITAITA Posts: 463Member
    Sorry, but I can't see the image? What could be the problem? Thanx.

    ITA :-)
  • vaynenickvaynenick UAEPosts: 1Member

    you must derive a new class that is based on the class of the control that you want, and you override the ProcessCmdKey().

    Syntax:

    C#:
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
    //handle your keys here
    }

    Full source..C# Arrow key detect

    Vayne

  • viettimetravelviettimetravel viet namPosts: 1Member

    I am also interested in this topic, need help

Sign In or Register to comment.