Stucked with Keyboard-GetDeviceData

Started by
1 comment, last by akuda 16 years, 10 months ago
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?
Advertisement
try the following after the kbuffer allocation line :

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

There is nothing that can't be solved. Just people that can't solve it. :)
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?

This topic is closed to new replies.

Advertisement