在Linux C中获取密钥状态

时间:2018-06-04 18:31:23

标签: c linux winapi keyboard arrow-keys

好了我已经搜索了很长一段时间了但是我根本找不到linux中GetKeyState()函数的替代品。我需要和想要的只是轮询箭头键,如果按下它们,执行一些操作。我的家用电脑是基于Linux的,我的学生' PC是基于Windows的,所以当我在那里工作时,我写了这段代码:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <dos.h>
#include <windows.h>
int x; int y, redraw;
int i; int l, xm, ym, op, as, b, ab;
short int display[10][10];

void draw() {
    if (redraw == 1) {
        system("cls");
        while (l < 10) {
            while (i<10) {
                if (display[i][l] == 0) { printf("="); }
                if (display[i][l] == 1) { printf("X"); }
                if (display[i][l] == 2) { printf("w"); }
                if (display[i][l] == 3) { printf("0"); }
                if (display[i][l] == 4) { printf("#"); }
                if (display[i][l] == 5) { printf("M"); }
                if (display[i][l] == 6) { printf("H"); }
                if (display[i][l] == 7) { printf("8"); }
                printf("|");
                i++;
            }
            i = 0;
            printf("\n");
            printf("-+-+-+-+-+-+-+-+-+-+");
            printf("\n");
            l++;
        }
        l = 0;
        redraw = 0;
    }
}

void getkeys() {
    while (b == 0) {
        if (GetKeyState(VK_LEFT) & 0x8000)
        {
            xm = -1;
            b = 1;
        }
        if (GetKeyState(VK_RIGHT) & 0x8000)
        {
            xm = 1;
            b = 1;
        }
        if (GetKeyState(VK_UP) & 0x8000)
        {
            ym = -1;
            b = 1;
        }
        if (GetKeyState(VK_DOWN) & 0x8000)
        {
            ym = 1;
            b = 1;
        }
        if (GetKeyState(VK_BACK) & 0x8000)
        {
            op = -1;
            b = 1;
        }
        if (GetKeyState(VK_RETURN) & 0x8000)
        {
            op = 1;
            b = 1;
        }

    } b = 0; redraw = 1;
}

void cursor() {
    display[x][y] = as;
    x = x + xm;
    xm = 0;
    y = y + ym;
    ym = 0;
    if (x >9) { x = 0; }
    if (y >9) { y = 0; }
    if (x <0) { x = 9; }
    if (y <0) { y = 9; }
    ab = display[x][y];
    as = ab;
    if (as == 0) {
        display[x][y] = 4;
    }
    if (as == 1) {
        display[x][y] = 5;
    }
    if (as == 2) {
        display[x][y] = 6;
    }
    if (as == 3) {
        display[x][y] = 7;
    }
    Sleep(100);
}

void main()
{
    while (i < 10) {
        while (l<10) { display[l][i] = rand() % 4; l++; } l = 0; i++;
    }
    redraw = 1;
    while (1) {
        draw();
        getkeys();
        b = 0;
        cursor();
    }
}

现在这基本上打印了一个数组和一个光标,但它确实使用了GetKeyState()函数,我在linux上找不到它的替代品。那么有没有任何简单的替代上述功能,是否有可能以某种方式使源代码多平台?提前谢谢。

0 个答案:

没有答案