"action on key hold" programs?

This is a discussion on "action on key hold" programs? within the C++ Programming forums, part of the General Programming Boards category; i need some help with a program,in which using a key press-n-hold (or simply hold),we can continue to execute any ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Location
    Bhubaneswar
    Posts
    3

    Question "action on key hold" programs?

    i need some help with a program,in which using a key press-n-hold (or simply hold),we can continue to execute any function till it is held or some other key is held to execute some different function.i tried making a program for such a purpose,but it has a glitch. let's consider as follows:

    key hold 'i' is used for executing a function A, and key hold 'k' is used for executing function B. on key holding 'i' , function A gets executed repeatedly.now while key 'i' is held, key 'k' is also held,so execution switches over to function B. here both keys 'i' and 'k' are held now. when key 'k' is left while 'i' still held pressed, the execution does not shift back to function A, which is expected from the program. is there any solution to it?

    the code is as follows:

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    void main()
    {
       int ch;
       clrscr();
       while(1)
       {
          if (kbhit())
          {
             ch=getch();
          }
          else
          {
             ch=0;
          }
          delay(35);      
          switch(ch)
          {
             case 105:
                printf("i");
                break;
             case 107:
                printf("r");
                break;
             case 27:
                exit(1);
          }
       }
    }

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    use getasynckeystate

    to improve readabiliy in your code, change ch to a char (optionnal) and compare ch to the char
    case 'i':
    and
    case 'k':

    instead on the numbers

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    24
    what you want to do is not possible unless using a special keyboard the normal keyboard can only do one at a time and cannot hold another

  4. #4
    Registered User
    Join Date
    Jul 2007
    Location
    Bhubaneswar
    Posts
    3
    it can be noted that this thing is possible in computer games (car racing ones life Need For Speed) where the driving controls are bound to arrow directional keys. when 'accelerate' and 'left ' keys are pressed n held, then a combined effect is achieved,and when 'left' control is released then only function bound to 'accelerate' takes place i.e. acceleration. isn't there any code/script or even an algorithm which can solve the purpose?

  5. #5
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    what you want to do is not possible unless using a special keyboard the normal keyboard can only do one at a time and cannot hold another
    You can do this, but it depends on the input method used. For example, in windows, there is an event for when a key is pressed and another event for when the key is released so you can trap multiple keys being pressed and held down at the same time.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    @siddharth2212
    I'm guessing from the many broken things in your code that you're using crusty old TurboC for DOS right?

    In which case, you're using the wrong too for the job.

    Using a proper win32 compiler, you'll have access to a much richer API which will be able to tell you the information you want.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by tbca View Post
    what you want to do is not possible unless using a special keyboard the normal keyboard can only do one at a time and cannot hold another
    For most desktop computers it falls down to the keyboard controller which must keep a track of the state of individual keys (on or off). The controller can report to the OS whether multiple keys are pressed or not, by recording the keyboard signals in a virtual keyboard matrix

    However, since C++ has no concept of a keyboard controller, it falls down to nonstandard libraries or compiler extensions to handle this behaviour.

    On systems where there is no keyboard controller, its behaviour can probably be mimic'ed to give the same effect (as far as the user is concerned).

  8. #8
    Registered User
    Join Date
    Jul 2007
    Location
    Bhubaneswar
    Posts
    3

    @ Bench82

    can you name some non standard libraries (as u mentioned )which can help me out??

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Probably <windows.h>
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Bench82 View Post
    For most desktop computers it falls down to the keyboard controller which must keep a track of the state of individual keys (on or off). The controller can report to the OS whether multiple keys are pressed or not, by recording the keyboard signals in a virtual keyboard matrix

    However, since C++ has no concept of a keyboard controller, it falls down to nonstandard libraries or compiler extensions to handle this behaviour.

    On systems where there is no keyboard controller, its behaviour can probably be mimic'ed to give the same effect (as far as the user is concerned).
    Having written code to simulate a keyboard controller, I can say that this is only half-true. The keyboard controller is responsible for receiving the keypresses from the keyboard (and sending out "turn on the CAPS-LOCK LED" type messages), but the keyboard controller itself doesn't know anything about for example CAPS-LOCK, SHIFT, ALT, CTRL. It just sends a "this key was pressed" and "this key was released" respectively from the keyboard.

    So if someone where to press A and hold it down, then B and hold that down, then release A and release B after that, the sequence would be:

    "Keydown A"
    "Keydown B"
    "Keyup A"
    "Keyup B"

    [Note 1]

    It is the responsibility of the next layer software to determine what keys have been pressed and for example modify the keys based on ALT, CTRL, SHIFT etc.

    [1] Actual data from the keyboard actually looks slightly different: each key has it's own "keycode", and the translation from keycode to character value is done by the software on the machine. This is how you can, in windows, switch from, say, a French to a UK keyboard with a few mouse-clicks. The keyboard itself knows nothing about which keys are doing what, and if you really wanted to, you could write a keymap (that's the translation from keycode to "ascii") that puts the alphabet in ABCDEF order on the keyboard, instead of the QWERTY/ZWERTY order that is most commonly used with "european" languages.

    --
    Mats

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you use this under DOS, then I would suggest that you read the keyboard directly from the keyboard controller (port 60h and 64h), and use a map of which keys are up and which are down, and then perform the relevant actions based on that.

    --
    Mats

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by tbca View Post
    what you want to do is not possible unless using a special keyboard the normal keyboard can only do one at a time and cannot hold another
    It CaNt? I WoNdEr HoW UpPeRcAsE WoRkS ThEn.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. POSIX/DOS programs?
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 02-28-2003, 04:42 AM
  4. heeeeeeellllllpppppppp!!!
    By DarkDays in forum C++ Programming
    Replies: 15
    Last Post: 12-09-2001, 08:43 PM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21