Source

pyGAP / game / src / input / win32 / keyboard.cpp

Full commit
#include "input/win32/keyboard.h"

#include <windows.h>


#include <cstring>
#include <cstdio>
namespace input {
namespace win32 {


Keyboard::Keyboard(::HINSTANCE hinstance, unsigned char* key_map):
    hinstance_(hinstance),
    key_map_(key_map)
{
    printf("Keyboard created\n");
}


Keyboard::~Keyboard()
{}


int Keyboard::get_value(int index)
{
    //TODO: define what is the range of value depending on various input devices
    return is_pressed(index) ? 1 : 0;
}


int Keyboard::get_key_code(int key)
{
    //int vkey = virtual_key_[key];
    //int code = ::MapVirtualKey(vkey, 0);
    //printf("esc: %02x (vkey: %02x)\n", code, vkey);
    //return key_code_[code];
    return key;
}


int Keyboard::get_pressed_key()
{
    char bit;
    int shr;

    for (int i = 0; i < 32; i++) {
        if (key_map_[i]) {
            for (shr = 0; shr < 8; shr++) {
                bit = key_map_[i] >> shr;
                if (1 & bit)
                    break;
            }
            return ((i << 3) | shr);
        }
    }
    return -1;
}


bool Keyboard::is_escaped_key(int key)
{
    for (int i = 0; i < sizeof(escaped_key_); i++)
        if (key == escaped_key_[i])
            return true;

    return false;
}


void Keyboard::get_key_symbol(int key, char* name)
{
    // All key using an escape sequence "e0" should pass a flag to GetKeyNameText
    int flags = is_escaped_key(key);
    int scan_code = scan_code_[key];

    ::GetKeyNameText((scan_code << 16) | (flags << 24), name, 32);
}


bool Keyboard::is_pressed(int key)
{
    bool pressed = 1 & (key_map_[key >> 3] >> (key & 0x07));
    return pressed ? true : false;
}


} // namespace win32
} // namespace input