Solved

How to detect status of keyboard (solaris)

Posted on 1997-11-13
5
445 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to check for shares on HP-UX 1 55
Arduino EDI - Programming language 3 80
Linux hostname change 2 56
Log File Creation with Header and Footer 17 63
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

863 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

24 Experts available now in Live!

Get 1:1 Help Now