Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 5 Jan 2000 15:45:05 -0800 (PST)
From:      Jim Shankland <jas@flyingfox.com>
To:        freebsd-hackers@FreeBSD.ORG, lyndon@orthanc.ab.ca
Subject:   Re: Reading kbd scancodes from userland
Message-ID:  <200001052345.PAA26271@biggusdiskus.flyingfox.com>
In-Reply-To: <200001052332.e05NWSr84505@orthanc.ab.ca>

next in thread | previous in thread | raw e-mail | index | archive | help
Lyndon Nerenberg <lyndon@orthanc.ab.ca> asks:

> Is it possible to read the raw keyboard scancodes from a userland
> process? I can't find an ioctl for this. (This is 3.3-RELEASE+PAO3,
> atkbd and syscons.)

Yup.  Save the following as kbddump.c, then:
	cc -O kbddump.c -o kbddump
	./kbddump

Jim Shankland
NLynx Systems, Inc.


#include <fcntl.h>
#include <stdio.h>
#include <machine/console.h>
#include <termios.h>
#include <errno.h>

main(int argc, char *argv[])

{
  int fd;
  unsigned char onecode;
  int oldmode;
  int newmode = K_RAW;
  struct termios old_t, new_t;
  int rr;

  if (argc > 1 && strcmp(argv[1], "-k") == 0)
    newmode = K_CODE;

  if ((fd = open("/dev/ttyv0", O_RDWR)) < 0) {
    (void) fprintf(stderr, "/dev/kbd0: %s\n", strerror(errno));
    exit(1);
  }
  if (ioctl(fd, KDGKBMODE, &oldmode) < 0) {
    (void) fprintf(stderr, "KDGKBMODE: %s\n", strerror(errno));
    exit(1);
  }
  printf("Old mode is %d\n", oldmode);
  if (tcgetattr(fd, &old_t) != 0) {
    (void) fprintf(stderr, "tcgetattr: %s\n", strerror(errno));
    exit(1);
  }
  new_t = old_t;
  cfmakeraw(&new_t);
  if (tcsetattr(fd, 0, &new_t) != 0) {
    (void) fprintf(stderr, "tcsetattr: %s\n", strerror(errno));
    exit(1);
  }
  
  if (ioctl(fd, KDSKBMODE, newmode) < 0) {
    (void) tcsetattr(fd, 0, &old_t);
    (void) fprintf(stderr, "KDSKBMODE: %s\n", strerror(errno));
    exit(1);
  }
  
  printf("Scancodes are in hexadecimal; program will terminate when the\r\n");
  printf("'a' key is released (scancode 9e) ...\r\n\n");

  while ((rr = read(fd, &onecode, 1)) > 0) {
    printf("Code: %02x\r\n", (unsigned) onecode);
    if (onecode == 0x9e)
      break;
  }
  if (rr < 0) {
    (void) fprintf(stderr, "keyboard read: %s\r\n", strerror(errno));
  }
  (void) tcsetattr(fd, 0, &old_t);
  if (ioctl(fd, KDSKBMODE, oldmode) < 0) {
    (void) fprintf(stderr,
                   "Danger, Will Robinson! Can't restore keyboard: %s\n",
                   strerror(errno));
    exit(1);
  }
  exit(0);
}


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200001052345.PAA26271>