输入方向键

Sfml Text Entered arrow keys

本文关键字:方向 输入      更新时间:2023-10-16

我正在用sfml制作主机风格的游戏。它是图形界面,但游戏本身包含主机。我使用文本输入事件在控制台写的东西,但我也想移动光标(控制台光标不是鼠标)与箭头键。更具体地说,在文本输入事件中,我使用event.text.unicode作为键值。但箭头键unicode值不工作在我的电脑。我用37表示左箭头,用39表示右箭头,但不行。这是我的控制台的关键进程功能。

void Console::update(int unicode) {
    if(unicode == 8) { // Backspace
        deleteLast();
    } else if(unicode == 13) { // Enter
        newLine();
    } else if(unicode == 37) { // Left arrow key
        currentX--;
        std::cout << "Left arrow" << std::endl;
    } else if(unicode == 39) { // Right arrow key
        currentX++;
        std::cout << "Right arrow" << std::endl;
    } else { // Normal characters
        buffer[(currentY == 0) ? currentX : (currentY * maxColumn) + currentX] = (char)unicode;
        currentX++;
        if(currentX == maxColumn - 1) {
            newLine();
        }
        std::cout << "[KEY TYPED] X: " << currentX << " Y: " << currentY << std::endl;
    }
}

Thanks in advance:)

编辑:我想我解决了。我使用KeyPressed事件来处理特殊的键。

对于有同样问题的人:
从Event类型变量中,首先检查它是否为键盘事件

if(event.type == sf::Event::KeyPressed)

然后检查它是否为箭头:

if(event.key.code == sf::Keyboard::Up)
    //do stuff
if(event.key.code == sf::Keyboard::Down)
    //do stuff