Solved

How to detect status of keyboard (solaris)

Posted on 1997-11-13
5
441 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
Comment Utility
man XQueryKeymap
0
 

Author Comment

by:byan
Comment Utility
Edited text of question
0
 
LVL 84

Expert Comment

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

Author Comment

by:byan
Comment Utility
B
0
 
LVL 1

Accepted Solution

by:
dhughes111797 earned 200 total points
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now