簡體   English   中英

在Linux上用C打開系統調用

[英]open system calls in C on linux

下面的代碼可能有幾個問題。 在搜索了在linux中獲取鍵盤輸入的方法之后在網上找到它。 我已經驗證了鍵盤輸入的正確事件。 它對我來說似乎有點可疑的原因是無論我放在文件路徑中,它似乎總是通過錯誤檢查(open調用返回大於0的東西)。 有些事情顯然是錯誤的,所以歡迎提出建議。

除非你將exe作為su運行,否則這將無法正常運行。

當我想讀取我的擊鍵時,我是否只是在無限循環中使用文件描述符上的fgets之類的東西(甚至可以工作)? 我希望它能夠不斷輪詢鍵盤輸入。 有關解碼鍵盤事件輸入的任何提示?

再次感謝! 我的這個項目可能過於雄心勃勃,因為我做了任何編碼已經很長時間了。

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <fcntl.h>
#include <linux/input.h>
#include <unistd.h>

// Edit this line to reflect your filepath

#define FILE_PATH "/dev/input/event4"

int main()
{
    printf("Starting KeyEvent Module\n");

    size_t file; //will change this to int file; to make it possible to be negative
    const char *str = FILE_PATH;

    printf("File Path: %s\n", str);

    error check here
    if((file = open(str, O_RDONLY)) < 0)
    {
        printf("ERROR:File can not open\n");
        exit(0);
    }

    struct input_event event[64];

    size_t reader; 
    reader = read(file, event, sizeof(struct input_event) * 64);

    printf("DO NOT COME HERE...\n");

    close(file);
    return 0;
}

問題出在這里:

size_t file;

size_t是無符號的,因此它總是> = 0

應該是:

int file;

open調用返回大於0的值

open返回int ,但你輸入一個無符號變量( size_t通常是無符號的),所以你無法檢測到它何時<0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM