Thread: DirectInput help

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    33

    DirectInput help

    Ive got a problem with using keyUp and keyDown.

    I want a frame to move when the key is down, but have it stop when the key is released, not when the key is just up.

    How do I do this?

  2. #2
    keyUp must be some custom function you've written. We'll need to see your input trapping code before any real advice can be given.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    33
    Yeah, I have a header and cpp for direct input just to make it easier, ill post them here

    Code:
    //CInput8.cpp
    
    #include "CInput8.h"
    
    LPDIRECTINPUT lpdi;
    LPDIRECTINPUTDEVICE m_keyboard;
    UCHAR keystate[256];
    UCHAR keypress_state[256];
    
    LPDIRECTINPUTDEVICE m_mouse = NULL;
    float mX = 0.0f, mY = 0.0f;
    DIMOUSESTATE mouse_state;
    
    // Initiates input system
    bool Init_CInput8(HINSTANCE h)
    {
    
    	if (FAILED(DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&lpdi, NULL)))
    		return false;
    	
    	for (int i = 0; i < 256; i++)
    		keypress_state[i] = 0;
    
    	return true;
    }
    
    
    bool Init_Keyboard(HWND hWnd)
    {
    	if (FAILED(lpdi->CreateDevice(GUID_SysKeyboard, &m_keyboard, NULL)))
    		return false;
    	if (FAILED(m_keyboard->SetDataFormat(&c_dfDIKeyboard)))
    		return false;
    	if (FAILED(m_keyboard->SetCooperativeLevel(hWnd, DISCL_BACKGROUND |
    		DISCL_NONEXCLUSIVE)))
    		return false;
    	if (FAILED(m_keyboard->Acquire()))
    		return false;
    	return true;
    }
    
    bool Acquire_Keyboard()
    {
    	if (FAILED(m_keyboard->Acquire()))
    		return false;
    	return true;
    }
    
    bool Unacquire_Keyboard()
    {
    	if (FAILED(m_keyboard->Unacquire()))
    		return false;
    	return true;
    }
    
    bool Read_Keyboard()
    {
    	if (FAILED(m_keyboard->GetDeviceState(sizeof(UCHAR[256]), (LPVOID)keystate)))
    		return false;
    	return true;
    }
    
    bool Release_Keyboard()
    {
    	SafeRelease(m_keyboard);
    	return true;
    }
    
    bool KeyDown(DWORD key)
    {
    	return ((keystate[key] & 0x80) ? true : false);
    }
    
    bool KeyUp(DWORD key)
    {
    	return ((keystate[key] & 0x80) ? false : true);
    }
    
    bool KeyPress(DWORD key)
    {
    	if (KeyDown(key))
    		keypress_state[key] = 1;
    
    	if (keypress_state[key] == 1)
    		if (KeyUp(key))
    			keypress_state[key] = 2;
    
    	if (keypress_state[key] == 2)
    	{
    		keypress_state[key] = 0;
    		return true;
    	}
    	
    	return false;
    }
    
    bool Init_Mouse(HWND hWnd)
    {
    	if (FAILED(lpdi->CreateDevice(GUID_SysMouse, &m_mouse, NULL)))
    		return false;
    	if (FAILED(m_mouse->SetCooperativeLevel(hWnd, DISCL_BACKGROUND |
    		DISCL_NONEXCLUSIVE)))
    		return false;
    	if (FAILED(m_mouse->SetDataFormat(&c_dfDIMouse)))
    		return false;
    	if (FAILED(m_mouse->Acquire()))
    		return false;
    	return true;
    }
    
    bool Acquire_Mouse(void)
    {
    	if (FAILED(m_mouse->Acquire()))
    		return false;
    	return true;
    }
    
    bool Unacquire_Mouse(void)
    {
    	if (FAILED(m_mouse->Unacquire()))
    		return false;
    	return true;
    }
    
    bool Read_Mouse(void)
    {
    	if (FAILED(m_mouse->GetDeviceState(sizeof(DIMOUSESTATE), 
    		(LPVOID)&mouse_state)))
    		return false;
    	return true;
    }
    
    void Get_Movement(float &m_x, float &m_y)
    {
    	m_x = mouse_state.lX;
    	m_y = mouse_state.lY;
    }
    
    void Get_Mouse_Coords(float &m_x, float &m_y)
    {
    	Get_Mouse_X(m_x);
    	Get_Mouse_Y(m_y);
    }
    
    void Get_Mouse_X(float &m_x)
    {
    	mX += mouse_state.lX;
    	if (mX < 0)
    		mX = 0;
    	
    	m_x = mX;
    }
    
    void Get_Mouse_Y(float &m_y)
    {
    	mY += mouse_state.lY;
    	if (mY < 0)
    		mY = 0;
    	
    	m_y = mY;
    }
    
    bool Button_Down(int mouse_button)
    {
    	return ((mouse_state.rgbButtons[mouse_button] & 0x80) ? true : false);
    }
    
    bool Button_Up(int mouse_button)
    {
    	return ((mouse_state.rgbButtons[mouse_button] & 0x80) ? false : true);
    }
    
    bool Release_Mouse()
    {
    	if (Unacquire_Mouse())
    		return false;
    	SafeRelease(m_mouse);
    	return true;
    }
    
    // shutdown input system
    bool Shutdown_CInput8(void)
    {
    	SafeRelease(lpdi);
    	return true;
    }
    Code:
    //CInput9.h
    
    #ifndef __CINPUT8_H__
    #define __CINPUT8_H__
    
    #define WIN32_LEAN_AND_MEAN				// trim the excess fat from Windows
    
    #include <windows.h>				// standard Windows app include
    #include <dinput.h>
    
    typedef unsigned char UCHAR;
    
    #define SafeRelease(x)	if (x) {x->Release(); x=NULL;}
    
    bool Init_CInput8(HINSTANCE h);
    bool Shutdown_CInput8(void);
    
    bool Init_Keyboard(HWND hWnd);
    bool Acquire_Keyboard();
    bool Unacquire_Keyboard();
    bool Read_Keyboard();
    bool Release_Keyboard();
    bool KeyDown(DWORD key);
    bool KeyUp(DWORD key);
    bool KeyPress(DWORD key);
    
    bool Init_Mouse(HWND hWnd);
    bool Acquire_Mouse(void);
    bool Unacquire_Mouse(void);
    bool Read_Mouse(void);
    bool Release_Mouse(void);
    void Get_Mouse_X(float &m_x);
    void Get_Mouse_Y(float &m_y);
    void Get_Mouse_Coords(float &m_x, float &m_y);
    void Get_Movement(float &m_x, float &m_y);
    bool Button_Down(int mouse_button);
    bool Button_Up(int mouse_button);
    
    // defines for mouse
    #define LEFT_BUTTON			0
    #define RIGHT_BUTTON		1
    #define MIDDLE_BUTTON		2
    
    #endif
    When I want to get a keydown I do this

    Code:
    if (KeyDown(DIK_W)){
    //Do stuff
    }
    Hope that helps

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectInput
    By Lionmane in forum Windows Programming
    Replies: 4
    Last Post: 11-12-2005, 10:43 PM
  2. DirectInput and Dev-C++
    By Niara in forum Game Programming
    Replies: 2
    Last Post: 03-11-2005, 09:20 AM
  3. DirectInput woes again....
    By VirtualAce in forum Game Programming
    Replies: 4
    Last Post: 05-25-2004, 03:02 PM
  4. DirectInput or WM_KEYDOWN question
    By Mecnels in forum Game Programming
    Replies: 4
    Last Post: 03-18-2003, 05:45 AM
  5. My DirectInput tutorial....
    By jdinger in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-18-2002, 11:32 PM