Solved

How to detect status of keyboard (solaris)

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

803 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