• Advertisement
Sign in to follow this  

Stucked with Keyboard-GetDeviceData

This topic is 3910 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I got some problems with buffered input of keyboard, here's the code: Here's the init:

  void Keyboard::init(LPDIRECTINPUT8 pDI, HWND hWND)
    {
		hWnd = hWND;

        LogManager& l = LogManager::getSingleton();
		l.log("Keyboard: Initializing keyboard...");

        KBuffer = (DIDEVICEOBJECTDATA*)malloc(sizeof(DIDEVICEOBJECTDATA) * buffersize);

		if(!(KBuffer)) l.log("Unable to allocate memory for keyboard buffer!");

        if(FAILED(
        pDI->CreateDevice(GUID_SysKeyboard, &defaultKeyboard, NULL)))
        {
			l.log("Unable to create keyboard device!");
        };

        if(FAILED(
        defaultKeyboard->SetDataFormat(&c_dfDIKeyboard)))
        {
			l.log("Unable to set data format for keyboard!");
        };

        if(FAILED(
        defaultKeyboard->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE)))
        {
			l.log("Unable to set a cooperative level of keyboard!");
        };

        {
            DIPROPDWORD         dipdw;

            dipdw.diph.dwSize = sizeof(DIPROPDWORD);
            dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
            dipdw.diph.dwObj = 0;
            dipdw.diph.dwHow = DIPH_DEVICE;
            dipdw.dwData = buffersize;
            if(FAILED(
            defaultKeyboard->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph)))
            {
				l.log("Unable to set a buffer for keyboard");
            };
        };

        if(FAILED(
        defaultKeyboard->Acquire()))
        {
			l.log("Unable to acquire a keyboard!");
        };

		l.log("Keyboard: Keyboard initialized.");
        };

And this is repeated every frame, in purpose of getting the input.
 void Keyboard::update()
    {
        DWORD dwItems = buffersize;
        DIDEVICEOBJECTDATA *act;

        switch(
        defaultKeyboard->GetDeviceData(sizeof(DIDEVICEOBJECTDATA),
                                         KBuffer, &dwItems, 0)
                )
        {
            case DI_OK:
                break;
            case DI_BUFFEROVERFLOW:
                LogManager::getSingleton().log("Keyboard buffer overflow!");
                break;
            default:
                LogManager::getSingleton().log("Unknown keyboard error!");
                break;
        };

        for(DWORD k = 0; k < dwItems; ++k)
        {
            act = &KBuffer[k];

            if (act->dwData & 0x80)
            {
                processKeyDown(act->dwOfs);
            }
            else
            {
                processKeyUp(act->dwOfs);
            };
        };


    };

I expected the processKeyDown function to get only values between 0-256, but it sometimes gets minus-something-huge. What's the error?

Share this post


Link to post
Share on other sites
Advertisement
try the following after the kbuffer allocation line :

memset(KBuffer, 0, sizeof(DIDEVICEOBJECTDATA) * buffersize);

Share this post


Link to post
Share on other sites
nope, not that.

I played a while with debugger, and found out, that
if(FAILED(
defaultMouse->Acquire()))

fails (I mean: the mouse is not acquired);

why's that?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement