2

编辑:经过更多代码修改后,错误仍然存​​在,修改后的代码显示:

KeyDown()

const int input_bit_num = 0x8000;
char keys[256];
bool KeyDown(int key)
{
    return (keys[key] & input_bit_num) != 0;
}

PollKeyboard()

LPDIRECTINPUTDEVICE8 di_keyboard;
void PollKeyboard()
{
    long result = di_keyboard->GetDeviceState(sizeof(keys), (LPVOID)&keys);
    char para[16];
    itoa(result, para, 17);
        if(result != DI_OK) MessageBox(NULL, para, "ERROR", MB_OK);

}

当我尝试将 MessageBox 放在KeyDown()if 语句中时(如下面的游戏循环所示),即使我停止按键,MessageBox 也会继续出现,即:我按下 ,“你想退出吗?” 消息框出现,我说不,它消失了,然后立即重新出现,好像我还在握着钥匙。

这是我的循环:

void GameRun(HWND hWnd) //called once every frame
{
    PollKeyboard();
    if(GetTickCount - start >= 30)
    {
        if(KeyDown(DIK_LEFT))
            MoveLeft();
        if(KeyDown(DIK_RIGHT))
            MoveRight();
    }

    if(d3ddev->BeginScene())
    {
        //rendering
    }

    if(KeyDown(DIK_ESCAPE))
    {
        //any MessageBox()
        int result = MessageBox(hWnd, "I'm causing so much trouble!", "IMMORTAL", MB_YESNOCANCEL);
        if(result == IDYES)
            //end
    }
}

编辑: 中的 catchPollKeyboard()显示序列53gd6bcc,但是我找不到它对应的错误代码。

编辑:经过另一次测试,我看到即使 MessageBox 不在 KeyDown() if 语句中,故障仍然发生。

编辑:经过更多测试,似乎 MessageBox 本身导致了故障。

4

2 回答 2

2

因为示例代码有效,所以程序中的其他东西会导致错误。尝试将下面的代码部分移动到你自己的代码中,直到它起作用,然后你就会知道哪一部分代码是罪魁祸首。

示例代码

好吧,巨大的代码块来了。这段代码对我来说是正确的。(Escape,以及所有其他键成功激活和停用)。它很大,有评论,并且很好地解释了事情。试试这个,如果它有效,我们将检查你程序的其他部分,如果没有,我只能给你“祝你好运”,然后拿你想要的:

// DirectInput
#define DIRECTINPUT_VERSION 0x0800
#include<dinput.h>

// Standard stuff
#include <iostream>
#include <stdexcept>
#include <sstream>
#include <string>

// Link from code, MSVC specific, could be done in project settings
#pragma comment(lib, "dinput8.lib")
#pragma comment(lib, "dxguid.lib")

// Utility lexical_cast, use Boost if possible.
// Simple replacement, converts a stream-able `T`
// to a string
template <typename T>
const std::string lexical_cast(const T& pValue)
{
    std::stringstream ss;
    ss << pValue;

    return ss.str();
}

// Utility function + macro to execute DirectX code with exceptions.
// Kinda ugly, but helpful for us.
void check_error(HRESULT pResult, const std::string& pFuncName)
{
    // DI_OK == S_OK, but S_OK is more general, so we'll use that
    if (pResult != S_OK)
    {
        throw std::runtime_error("Error executing: " + pFuncName +
                                "! Returned: " + lexical_cast(pResult));
    }
}

// Macro, makes calling the function easier. It is wrapped in
// an `if` statement for reasons outlined in:
// http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.5
#define CHECK_ERROR(x) if (true) { check_error(x, #x); } else (void)0

// The above gives the warning:
// "warning C4127: conditional expression is constant", disable below:
#pragma warning(disable: 4127)

// Manages input
class input_manager
{
public:
    // Constants
    static const int NumberKeys = 256;

    // Creation
    input_manager(void)
    {
        // Create input and keyboard (like I said, ugly macro, but helpful :] )
        CHECK_ERROR(DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION,
            IID_IDirectInput8, reinterpret_cast<void**>(&_input), 0));


        CHECK_ERROR(_input->CreateDevice(GUID_SysKeyboard, &_keyboard, 0));
        CHECK_ERROR(_keyboard->SetDataFormat(&c_dfDIKeyboard));
        CHECK_ERROR(_keyboard->Acquire());
    }

    ~input_manager(void)
    {
        // Free resources. Note: Many programmers
        // unnecessarily wrap this stuff in
        // `if (_keyboard !=0)`, and then
        // `_keyboard = 0`. This is completely unnecessary,
        // because destructors are only run one time.

        // Also, I can assume they are valid, because if they
        // weren't, we wouldn't be here (an exception would have
        // been thrown)

        _keyboard->Unacquire();
        _keyboard->Release();
        _input->Release();

        // Also, if we wrapped this into a nice RAII class, we wouldn't
        // be forced to write a destructor, but this is outside the scope.
        // Feel free to ask how; additionally, since we're on the topic, if you'd
        // like more tips handling input (I've written PLENTY of input managers)
        // I'm free for asking about things like testing for triggers rather than pressed
        // ("was it pressed, regardless if it's being held now" versus
        // "is it being pressed"), etc.
    }

    // Operations
    void update(void)
    {
        CHECK_ERROR(_keyboard->GetDeviceState(NumberKeys, reinterpret_cast<void*>(&_keys)));
    }

    // Query
    bool key_pressed(int pKey) const
    {
        return test_key(pKey);
    }

    // Might wrap into an operator[] for convenience.

private:
    // Constants
    static const int PressMask = 0x80;

    // Sorry for the confusion, but indeed, with
    // `char`s the mask is simply 0x80.

    // Utility
    bool test_key(int pKey) const
    {
        return (_keys[pKey] & PressMask) != 0;
    }

    // Members
    LPDIRECTINPUT8 _input;
    LPDIRECTINPUTDEVICE8 _keyboard;

    char _keys[NumberKeys];
};

void test_keys(const input_manager& input)
{
    bool anyPressed = false;

    for (unsigned i = 0; i < input_manager::NumberKeys; ++i)
    {
        if (input.key_pressed(i))
        {
            std::cout << "Pressing: " << i << std::endl;

            anyPressed = true;
        }
    }

    if (!anyPressed)
    {
        std::cout << "No keys pressed." << std::endl;
    }
}

void execute(void)
{
    input_manager input;

    std::cout << "Press Q to quit." << std::endl;

    bool running = true;
    while (running)
    {
        input.update();

        if (input.key_pressed(DIK_Q))
        {
            running = false;
        }

        test_keys(input);

        Sleep(0); // give some processor time
    }
}

int main(void)
{
    // Place real code in an execute function, so main
    // is clean and ready to catch exceptions:
    try
    {
        execute();
    }
    catch (const std::exception& e)
    {
        // Error!
        std::cerr << "Unhandled exception:" << e.what() << std::endl;
    }
}

老建议:

尝试从 GetDeviceState 中获取返回值:

HRESULT result =                              // v Prefer C++-style casts
    di_keyboard->GetDeviceState(sizeof(keys), reinterpret_cast<void*>(&keys);

if (result != DI_OK)
{
    // uh-oh
    std::cout << result << std::endl;
}

将其与此处的表格进行比较。

老半答案:

在 Extra Stuff 部分中编辑代码后不久,我意识到了错误,抱歉我没有早点发现它。您正在测试错误的位:)

观察:

//                                     v HERE! Should be 0x8000, not 0x80.
return (GetAsyncKeyState(pKeyCode) & 0x8000) != 0;

试试看:

int KeyDown(int key)
{
    return (keys[key] & 0x8000);
}

此外,这应该被移动到一个常数以避免幻数:

// somewhere, probably in the private section of the class or in a detail namespace:
static const int PushedMask = 0x8000;

// code reads better:
int KeyDown(int key)
{
    return (keys[key] & PushedMask);
}

最后,在 C++ 中你有一个bool类型,所以好好利用它!

// v here
bool KeyDown(int key)
{
    return (keys[key] & PushedMask);
}

我知道 Visual Studio 会警告这种从intto 的转换bool,所以你可以摆脱它,同时也让你的意图更清晰:

bool KeyDown(int key)
{
    return (keys[key] & PushedMask) != 0; // or == 1, your choice
}

额外的东西:

试试下面的代码:

#include <iostream>
#include <windows.h>

bool key_pressed(int pKeyCode)
{
    return (GetAsyncKeyState(pKeyCode) & 0x8000) != 0;
}

void test_keys(void)
{
    for (unsigned i = 0; i < 255; ++i)
    {
        if (key_pressed(i))
        {
            std::cout << "Pressing: " << i << std::endl;
        }
    }
}

int main(void)
{
    bool running = true;
    while (running)
    {
        if (key_pressed(VK_ESCAPE))
        {
            running = false;
        }

        test_keys();

        Sleep(0);
    }
}

这对我有用(响应所有键,退出时退出)。的最小测试用例GetAsyncKeyState。如果这不起作用请在您的评论中添加操作系统、键盘等。

于 2009-10-19T21:26:13.420 回答
1

如果您创建 MessageBox(Null,...) 在创建后您将无法控制窗口。IE,按下键时窗口不会消失。

至于为什么一直出现,似乎与此有关:

const int input_bit_num = 0x8000;
char keys[256];
bool KeyDown(int key)
{
    return (keys[key] & input_bit_num) != 0;
}

键由 1 字节长的字符组成,而 input_bit_num 是 2 字节的值。虽然我真的不知道您要查找的是哪一位(0xff - 0x00 是 1 个字节的域)。

老实说,我很惊讶您的代码运行,除非 & 操作延续到 keys[key-1] 在这种情况下,任何 KeyDown 都是未定义的,并且当 key 为 0 时 KeyDown(...) 特别危险。

于 2009-10-21T22:42:05.457 回答