Problem with Managed DirectInput

dxfoo

Newcomer
Joined
Sep 15, 2005
Messages
4
Location
Salem, OR
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;
        }
    }
}
 
Nate Bross said:
You might want to see this thread http://www.xtremedotnettalk.com/showthread.php?t=93639. I know that code works because I am using it in a project I'm working on. It gets the input in a different way. I don't know which is more effecient, but that example works.

Thanks for the input; however, I dramatically changed my code after learning how to apply 3d to 2d. For the input, I was able to do this...

Code:
        // Call this before showing form. 
        public void InitializeInput()
        {
            // Create our keyboard device
            deviceInput = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
            deviceInput.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
            deviceInput.Acquire(); 
        }

        // Game loop method
        private void UpdateInputState()
        {
            KeyboardState state = deviceInput.GetCurrentKeyboardState();

            if (state[Key.Escape])
                this.Close();
            if (state[Key.S])
                PC.Y += 10;
            if (state[Key.W])
                PC.Y -= 10;
            if (state[Key.A])
                PC.X -= 10;
            if (state[Key.D])
                PC.X += 10; 
        }
 
Back
Top