NO

Author Topic: want to check if a keyboard key has been press  (Read 5199 times)

fallen

  • Guest
want to check if a keyboard key has been press
« on: January 18, 2015, 08:15:15 PM »
Hello Everybody,
Here's the background, started coding C back in 1986 on Xinux, then AT&T Unix, then MS-DOS, then Windows 95. As the years have passed I'll been pulled into more  ::) analytic work and package programming: MS Access, Crystal Reports, SAS, SQL MS VBasic, etc... Back in the day, I coded with MS C version 5.0. I wrote a few dozen utilities that have over the years, stopped working, not all, but some. No big deal, the net provided other replacement utilities for most of them, however, one I have really missed was a util called wait.exe. I would use this util in batch files to allow a timed pause to occur. It would take a parameter of seconds and it would do either of two things; wait for the number of seconds and return, or if a keyboard key was pressed, it would return.

More background, my primary PC with all my source, compiler, etc.. has a harddrive crash  :'( a few years after I got out of C (around 2000) . Well of course I was backing it up and had multiple backups, because I had already learned that lesson! However, since I really didn't need it right away, I kind of put off restoring it  :-\ --- another lesson learned. Now I still have the backup tape and the old PC with the tape drive. However, the backup tapes are somewhere in a box, somewhere in storage and I could find it, but...

So now to the present. I haven't been able to find a replace wait.exe util that does the two things I need it to do, so about a week ago, I thought that I would just re-write it. Reviewed the available C compilers and selected Pelles C :). Then searched for some source code on the web and started to compile a few timer programs. Got them working, so then started to code the "check the keyboard for a key being pressed" into the timer loop. The functionality that I want is not to wait on a key to be pressed, but to check if a key has been pressed. If no key has been pressed, continue to the next step. If this functionality can be done with ftn calls like _cgets, _cscanf, _eof, _tell, etc.. then my problem would be solved, just give me a sample of the code. It took a few days of working on this part of the code, then I remembered that I had written a few .obj libraries and one of those .obj files was what I used to inspect the keyboard buffer to see if a key was waiting. This .obj ftn was directly accessing the keyboard interrupt. Then I started doing more research on the forum here and understand that (I think) there is no direct interrupt interface, at least not the way I would need it to work. The forum has been very informative and has helped me understand the reasons why my old MS-DOS utils have stopped working and that I will need to write code as Win32 or Win64 console programs.

So now to the help I need
. Are there ways, in Pelles C, to check to see if a key has been pressed without waiting on a "Enter" key to be pressed? In other words, call the ftn and the ftn returns either 0 or -1 based on if a key in sitting in the keyboard buffer. If someone can tell me if writing the wait.exe console ftn, with the two options of ending the util is possible in Pelles C, I would really appreciate it. If someone has already written the wait.exe ftn and would be willing to share the code I would really, really appreciate it. If someone just has the code for the ftn to check to see if a keyboard key has been pressed and would share the code I will buy you a drink (of your choice) the next time we meet!

czerny

  • Guest
Re: want to check if a keyboard key has been press
« Reply #1 on: January 18, 2015, 09:20:35 PM »
What about _kbhit()?

fallen

  • Guest
Re: want to check if a keyboard key has been press
« Reply #2 on: January 18, 2015, 09:24:32 PM »
caerny,
Yes, that works. I found it in the help file and just came back in to close this request. Thanks,

fallen

  • Guest
Re: want to check if a keyboard key has been press
« Reply #3 on: January 20, 2015, 04:30:13 PM »
caerny,
Thanks for your reply. If I hadn't posted, I'm sure that it would have been another 3 days of searching and even then, who knows if I would have found the _kbhit ftn. Thanks again.

I've completed the wait.exe util and have attached the source and project files. I figure that some other coders might like to have an example of a utility that will compile and works. I left all my "will this work" and debugging stuff in so people can play around with it like I did. I plan on adding the getopt ftn to process the argv options and adding the functionally of passing in a new string to replace the default message as an option. But, it may take a while, so thought I would go ahead and post the code now, since a while for me have in the past been between a few hours and a few years!

czerny

  • Guest
Re: want to check if a keyboard key has been press
« Reply #4 on: January 20, 2015, 06:51:52 PM »
This is another small example. If no parameter is given, this waits until keypressed.
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>

int main(int argc, char **argv)
{
clock_t start;
double stop;

if (argc == 2)
{
stop = strtod(argv[1], NULL);
if (stop > 0.0) {
start = clock();
while (!_kbhit())
{
if ((double)(clock() - start)/CLOCKS_PER_SEC >= stop) break;
}
}
else puts("usage: wait [seconds]");
}
else while (!_kbhit());

return 0;
}

fallen

  • Guest
Re: want to check if a keyboard key has been press
« Reply #5 on: January 21, 2015, 03:42:42 AM »
czerny,
Sweet! That code would have saved me a few days of research. It will also show people that there are a lot of ways to reach the same end. I also just figured out how you displayed the code within the post. Well this little project has again reminded me why I only keep up with the languages that I'm paid to use. About 7 days of my personnel time for a simple little program. I had to research the compiler, download and install the Pelles C environment, learn the IDE, find sample source code, research the language syntax (again), code/test/debug. All for one little utility that will pause the screen so a user can read the screen for a few seconds. Needless to say, I have been really missing that damn little utility a lot to do all of this work!

This project reminds me of all the times I thought I could do a quick problem fix with AWK or SED or Regular Expressions. Yes it would be a one to two day manual project, but with AWK or SED it would be an hour project, (that is) if you are current in either one of them, but it always seems that it takes just a bit longer. Thankfully these project haven't run over the manual time limit, so I still get to practice coding.

czerny

  • Guest
Re: want to check if a keyboard key has been press
« Reply #6 on: January 21, 2015, 07:58:16 AM »
Sweet! That code would have saved me a few days of research.
Oh, sorry! But I wouldn't take you the fun to do it yourself!

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: want to check if a keyboard key has been press
« Reply #7 on: January 22, 2015, 09:07:27 AM »
I don't like solutions like yours, because it runs with full CPU usage the full waiting time for doing nothing.
Here is my solution (a modification of czenys one), which should use less CPU.
« Last Edit: January 22, 2015, 09:11:19 AM by AlexN »
best regards
 Alex ;)

czerny

  • Guest
Re: want to check if a keyboard key has been press
« Reply #8 on: January 22, 2015, 11:51:48 AM »
I don't like solutions like yours, because it runs with full CPU usage the full waiting time for doing nothing.
Here is my solution (a modification of czenys one), which should use less CPU.
Your sleep granularity is a little bit large. If the user presses a key after 1+epsilon seconds, the program stops after 2 seconds.
I wouldn't do the '.' output or at least not to stdout.

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: want to check if a keyboard key has been press
« Reply #9 on: January 22, 2015, 02:07:31 PM »
I don't like solutions like yours, because it runs with full CPU usage the full waiting time for doing nothing.
Here is my solution (a modification of czenys one), which should use less CPU.
Your sleep granularity is a little bit large. If the user presses a key after 1+epsilon seconds, the program stops after 2 seconds.
I wouldn't do the '.' output or at least not to stdout.
I think the granularity is good enough for the most reasons (and the most other compiler I tried, use there as parameter ms instead of seconds).
I like the points, but you can remove them. ;)
best regards
 Alex ;)