File: keyboard.c

package info (click to toggle)
pdmenu 1.2.96
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 840 kB
  • ctags: 250
  • sloc: sh: 3,048; ansic: 2,037; makefile: 131; perl: 58
file content (40 lines) | stat: -rw-r--r-- 898 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* 
 * Copyright (c) 1995-1999 Joey Hess (joey@kitenet.net)
 * All rights reserved. See COPYING for full copyright information (GPL).
 */

#include "global.h"
#include "slang.h"
#include <unistd.h>

#define TIMEOUT 2 /* 2/10 of a second */

/* Get a key from keyboard, handling escape properly. */
int getch (void) {
  int ch;

  if (tcgetpgrp(0) == -1) return 033; /* Detect if the terminal went away. */
  
  while (SLang_input_pending(1000) == 0)
    continue;

  ch = SLang_getkey();

  if (ch == 033) {	/* escape */
    if (SLang_input_pending(TIMEOUT) == 0) {
      return 033;
    }
    else {
      /* If two or more escapes are queued up, assume the first is a
       * "real" escape, and not part of some escape sequance. */
      int ch2 = SLang_getkey();
      SLang_ungetkey(ch2);
      if (ch2 == 033) {
	return 033;
      }
    }
  }

  SLang_ungetkey(ch);
  return SLkp_getkey();
}