Solved

keyboard hit..

Posted on 1998-05-26
11
413 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
Comment Utility
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:yolis
Comment Utility
Thanx ALOT!

0
 

Author Comment

by:yolis
Comment Utility
Thanx ALOT!

0
 

Author Comment

by:yolis
Comment Utility
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
Comment Utility
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
Comment Utility
_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
Comment Utility
thanks, I will try to search for it..

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

728 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

10 Experts available now in Live!

Get 1:1 Help Now