I press down 'S' on the keyboard, and the character still sits at the same previous position. Why is this?
Code:
#region Using Clauses
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectInput;
#endregion
namespace MDX_Application
{
public partial class Form1 : Form
{
private Device device = null; // DirectInput device
private bool running = true; // Game loop control variable.
Common PC;
public Form1()
{
InitializeComponent();
// Make a simple player object.
PC = new Common(50, 50);
PC.img = Image.FromFile("C1_Stand.png");
// Set the window style.
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
}
protected override void OnClosed(EventArgs e)
{
running = false;
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
// Draw Player to the Screen
g.DrawImage(PC.img, PC.x, PC.y);
this.Invalidate();
}
public void InitializeInput()
{
device = new Device(SystemGuid.Keyboard);
device.SetCooperativeLevel(this, CooperativeLevelFlags.Background |
CooperativeLevelFlags.NonExclusive);
device.Acquire();
while (running)
{
UpdateInputState();
Application.DoEvents();
}
}
private void UpdateInputState()
{
foreach (Key k in device.GetPressedKeys())
{
if (k == Key.S) { PC.y += 10; }
}
}
}
class Common
{
public Image img = null;
public int x, y;
public Common(int x, int y)
{
this.x = x;
this.y = y;
}
}
}