Is this Multi-Threading?

This is a discussion on Is this Multi-Threading? within the C++ Programming forums, part of the General Programming Boards category; I want it to constantly run something UNTIL it receives a keypress like this Code: for ( ;; ) { ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Is this Multi-Threading?

    I want it to constantly run something UNTIL it receives a keypress like this

    Code:
    for ( ;; )
    {   
         Beep(x,10);
         _getch();
    }
    except that WAITS for the keypress before continuing with the loop. I need it to loop until it gets a keypress, effectively constantly beeping until a keypress. Basically, the keypresses will change the value of x, allowing the user to change the pitch.

    Can anyone please help me?

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    No, that's not Multi-Threading, it acts i little like it
    Could you tell use what compiler you're using?
    What headers?
    What kind of program?

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    That sounds like it would work, but how do I get it to store that? How could I get it to store the keypress (when it comes?)

    I need to have it check if it was

    W
    A S D

    I was going to make it be the arrow keys, but you need to have two ASCII values and I don't know how to do that.

    Thanks!

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    kbhit()

    Yeah, I use kbhit() like vVv shows every day, 'cause we run test loops that run 'till a key is pressed. I was thrilled when I discovered kbhit()!!!

    I think that after you exit the loop getch() will read your keypress that's left in the buffer... not sure... you'll have to try it, or wait for an answer from sombody who knows what he's talking about!

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    char which;
    do
    {
       while (!kbhit())
       {
           // Do something
       }
       which = tolower(getch());
    }
    while (which != 'w' || which != 's' || which != 'a' || which != 'd');

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Eibro, thats great, but one question: would it be simple to change that to the arrow keys? I know that they require two ASCII values, or at least seem to.

  7. #7
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Well multi threadin is always better and more advantageous.. using kbhit() for simple apps will make it work well... But the fact remains that only one instruction is being executed at any given time.....

    I wrote many games that are animated using kbhit() but the problem was when a key was pressed the statements inside the (!kbhit()){ } is run.. and in case you use a delay or something here the entire animation slows down.. but a small price to pay for that advantage........

  8. #8
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Eibro, thats great, but one question: would it be simple to change that to the arrow keys? I know that they require two ASCII values, or at least seem to.
    Strangely enough... I simply set mine up to look for a 72 for UP and it recognized it. Problem is you need to find a way for it to not think that's a capital H.

    Do this:

    Code:
    if (key_pressed = 0)
    {
      zero_pressed = 1;
    }
    switch (key_pressed)
    {
      case 72:
        if (zero_pressed == 1)
        {
          key_pressed = 1000;  /* UP */
          zero_pressed = 0;
        }
        else
        {
          key_pressed = 72;
        }
        break;
      case 75:
        if (zero_pressed == 1)
        {
          key_pressed = 1001; /* LEFT */
          zero_pressed = 0;
        }
        else
        {
          key_pressed = 75;
        }
        break;
      case 77:
        if (zero_pressed == 1)
        {
          key_pressed = 1002; /* RIGHT */
          zero_pressed = 0;
        }
        else
        {
          key_pressed = 77;
        }
        break;
      case 80:
        if (zero_pressed == 1)
        {
          key_pressed = 1003; /* DOWN */
          zero_pressed = 0;
        }
        else
        {
          key_pressed = 80;
        }
        break;
    }
    I've typed this from memory, so I don't quite remember if it works or not. But if it did, I think it was virtually error free. The zero_pressed takes care of the problem of it thinking the arrowkeys are letters (up is H and something is M, but I don't really remember).
    Last edited by Frobozz; 01-09-2003 at 11:59 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. overlapped I/O and multi threading
    By krishnampkkm in forum C++ Programming
    Replies: 2
    Last Post: 06-22-2009, 04:27 PM
  2. Multi Threading
    By beon in forum C Programming
    Replies: 5
    Last Post: 12-04-2006, 09:21 PM
  3. Replies: 6
    Last Post: 06-02-2006, 09:32 AM
  4. starting to learn multi threading
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2004, 02:44 PM
  5. Multi Threading
    By IceBall in forum C Programming
    Replies: 7
    Last Post: 07-13-2003, 04:01 PM

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