Solved

How to detect status of keyboard (solaris)

Posted on 1997-11-13
5
447 Views
Last Modified: 2013-12-05
Hi, Everyone who can help me,

I need to detect the  status of keyboard. In fact, what I want to know is: how to record the time duration of pressing a key. Getche or getch will accept keyboard input, but as soon as a key is pressed, the function will return a value. I need to realize some functions like DOS keyboard interrupt which can tell me when a key is pressed, when it is released, etc. My working platform is SUN workstation, Solaris 2.5.1

Thanks,

--
Yan, Bo

ECECS Dept. University of CincinnatiC
0
Comment
Question by:byan
  • 2
  • 2
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1256316
man XQueryKeymap
0
 

Author Comment

by:byan
ID: 1256317
Edited text of question
0
 
LVL 84

Expert Comment

by:ozo
ID: 1256318
Is your keyboard /dev/kbd?  Try ioctl(kb,KIOCTRANS,TR_NONE )
0
 

Author Comment

by:byan
ID: 1256319
B
0
 
LVL 1

Accepted Solution

by:
dhughes111797 earned 200 total points
ID: 1256320
PGP gets the interval between key presses (for setting up
randomness). You may want to look at this code.  Also, if
you are programming in X11 you can use events to notify you
when a key has been pressed and released. see XEvent, XAnyEvent
man pages. Also, 2.5.1 comes with Motif in /usr/dt so you can
use the Motif functions to make it easier.

Here's a simple codeblock:

#include <time.h>
#include <X11/Xlib.h>
 
 
void main()
{
    Display *d;
    Window w;
    XEvent e;
    double frac;
    struct timespec time1, time2;
 
    d = XOpenDisplay(NULL);
    w = XCreateSimpleWindow(d, XDefaultRootWindow(d), 10, 10, 100, 100, 2, 0,
0);
    XMapWindow(d, w);
    XSelectInput(d, w, KeyPressMask | KeyReleaseMask);
 
    while (True)
    {
        XNextEvent(d, &e);
 
   
        if (e.type == KeyPress)
            clock_gettime(CLOCK_REALTIME, &time1);
        else if (e.type == KeyRelease) {
            clock_gettime(CLOCK_REALTIME, &time2);
            frac = ((double) time2.tv_nsec - time1.tv_nsec) / 1000000000;
            if (frac < 0) {
                frac++;
                time2.tv_sec--;
            }
            printf("duration %2.2f seconds\n", (double) time2.tv_sec -
                time1.tv_sec + frac);
        }
    }
}

Also, see man -s7 kb, man -s7m kb

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
can't umount nfs share after server goes offline... 4 92
Python Assistance 7 86
Wrap Oraccle SQL*Plus executable Command 4 95
lunix and unix command 21 87
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question