gpt4 book ai didi

c - 在 linux 上用 C 语言打开系统调用

转载 作者:太空宇宙 更新时间:2023-11-04 08:45:50 25 4
gpt4 key购买 nike

下面的代码可能有几个问题。在搜索一种在 linux 中获取键盘输入的方法后,在网上找到了它。我已经验证了键盘输入的正确事件。对我来说它看起来可疑的原因是不管我在文件路径中放了什么,它似乎总是通过错误检查(open 调用返回大于 0 的值)。显然有什么地方不对,欢迎提出建议。

除非您以 su 身份运行 exe,否则它不会正确运行。

当我想读入我的击键时,我是否只是在无限循环中对文件描述符使用类似 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;

关于c - 在 linux 上用 C 语言打开系统调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21704139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com