Solved

keyboard hit..

Posted on 1998-05-26
11
416 Views
Last Modified: 2008-03-17
Hi!
I wanted to know if there is a way to cancel the delay in the keystrokes? for example:
if (kbhit()) c=getch();
say the user, presses left, so x-=10.
and if the user continues to press the left key, then continue wiith x-=10.
but there is a delay, right after the user presses, and until it starts to do c-=10 for the second time.
i know that i can minimize the delay with the command (in dos prompt):
mode con rate=32 delay=1

but there is still a delay!!!
how do i cancel it totally?!
  thanks, Yoli.

0
Comment
Question by:yolis
  • 5
  • 5
11 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1164746
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1164747
No, you can't cancel the delay between the initial keystroke and and the first repeated key.  This delay is produced a chip in the keyboard itself.  However, you can program the chip to reduce the delay to 250 ms, which is 1/4 of a second, from a DOS program.

continues
0
 
LVL 22

Expert Comment

by:nietod
ID: 1164748
The following assembly code will set keyboard rate to its highest value and the delay to its lowest value.

__asm
{
   MOV AX,0305H
   MOV BX,0
   INT 16H
}
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 84

Expert Comment

by:ozo
ID: 1164749
You could also emulate any rate you want by tracking the key-down and key-up events separately.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1164750
I beleive this is for a DOS program, so there are no key-down and key-up events.  However you could hook the keyboard interrupt and do the same thing.  However, My experience has been that this is not a great idea.  Better to rethink your design and examine all options before going in that direction.
0
 

Author Comment

by:yolis
ID: 1164751
Thanx ALOT!

0
 

Author Comment

by:yolis
ID: 1164752
Thanx ALOT!

0
 

Author Comment

by:yolis
ID: 1164753
Hmm... the assembly code, is not good enough, there is still a delay..

so, I tried to do track the key up and key down events saparetly using this code:

int movment=0;
while (ch!=27) {
movment=kbhit();
if (movment==1) {
do the stuff i want when there is a kbhit()...
}
if (!kbhit()) movment==0;
}

why isnt this working?
please help..

0
 

Author Comment

by:yolis
ID: 1164754
a correction for the code in the comment i wrote before, here is the function:
int game()
{
  int i2;
  char ch;
  int movment=0;
  int bla=0;
  while (ch!=27)
  {
    move_enemies();
    if(alive>=1) for(i2=0;i2<10*speed;i2++)
    {
      delay(1);
      if(yfire!=1 && i2%10==0) fire();
      if(i2%20==0) fire_enemies();
      if (movment==0)
      { movment=kbhit(); bla=0; }
      if (movment!=0)
      {
      if (bla==0) { ch=getch();
      bla=1;  }
      switch(ch)
      {
        case 75: x-=10; draw_me(); break;
        case 77: x+=10; draw_me(); break;
        case 32: clearfire(); yfire=2; xfire=x; break;
        case 27: X=2;
      }
      }
      if (!kbhit())  movment=0;


      if(X!=0) return(X);
    }
    if(alive==0) return (3);
  }
  return 0;
}

0
 
LVL 22

Expert Comment

by:nietod
ID: 1164755
_kbhit just checks the DOS key queue to see if there is a waiting key.  Your problem is the delay in keys being placed in there.

Your best solution is to handle the keyboard interrupt dirrectly, or at least to hook it.  It is called when any key pressed or released, so you can use it to keep track of what keys are down.

I reccomend you get a hold of "PC interrupts" by Ralph Brown if you don't have it already.  You will need to know how to handle int 9 and how to communicate with the keyboard to find out what key is down.
0
 

Author Comment

by:yolis
ID: 1164756
thanks, I will try to search for it..

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

810 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