获取键盘按键值

很久以前的代码拿出来了!熟悉linux键盘驱动的这段代码太容易啦!


/*获取键盘的按值*/
#include <stdio.h>
#include <linux/input.h>
#include <stdlib.h>
#include <fcntl.h>
#include <assert.h>
struct info
{
	struct timeval time;
	unsigned short type;
	unsigned short code;
	unsigned int value;
};
int main()
{
	int fd;
	struct info node;
	fd = open("/dev/input/event0",O_RDWR);
	if(fd<0)
	{
		printf("open error!\n");
	}
	//assert(fp);
	while(1)
	{
		long i=0;
		fflush(stdout);
		read(fd,&node,sizeof(struct info));
		if(node.type == EV_KEY)
		{
			printf("code :%d\n",node.code);
			printf("value: %d\n",node.value);
		}
	}
	
}


您好,可以使用MATLAB中的函数"getkey"来获取键盘键值。以下是一个示例代码: ```matlab function key = getkey(timeout) % GETKEY - get a keypress % key = GETKEY waits for a keypress and returns a single key. This % function is a replacement for MATLAB's built-in GETKEY function, which % does not work on all platforms. % % key = GETKEY(TIMEOUT) waits for a keypress for TIMEOUT seconds. If no % key is pressed within that time, an empty matrix is returned. % % See also INPUT, GETCH. % % Written by David Reiss <dreiss@github.com> % Modified by Xiangrui Li <xiangrui.li@gmail.com> % This function is a modified version of GETKEY by David Reiss % (http://www.mathworks.com/matlabcentral/fileexchange/7465-getkey), % which is itself a modified version of GETKEY by Oleg Komarov % (http://www.mathworks.com/matlabcentral/fileexchange/7465-getkey), % which in turn is inspired by a USENET posting by Malcolm McLean % (http://www.mathworks.com/matlabcentral/newsreader/view_thread/10104). % Check the system to see if Java is available. if ~usejava('awt') error('GETKEY requires Java to run.') end % Set default values for the input arguments. if nargin < 1 timeout = inf; end % Get the start time. start_time = tic; % Initialize the key value. key = []; % Loop until a key is pressed or the timeout is exceeded. while isempty(key) && (toc(start_time) < timeout) % Check for a keypress. drawnow; key = get(gcf, 'CurrentCharacter'); end ``` 使用该函数,可以在MATLAB中获取键盘按键的值。例如,以下代码将等待用户按下任意键,并将按键的值存储在变量"key"中: ```matlab key = getkey(); disp(key); ``` 希望对您有所帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值