Thread: Time measurement and sound

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    16

    Time measurement and sound

    Hello,

    first of all, i apologize if my english is not accurate.

    Now the question,

    is the following possible in C/C++:

    i write it in (beginners) pseudo-code.

    // start

    cout << "Enter: ";
    m = getch(); // i need an input-function wich processed every keypress
    // instantly

    clock the time of how long the key is pressed

    keypress = the time of how long the key was pressed

    if (keypress < 0.5 sec)
    cout << "a";
    if (keypress > 0.5 sec)
    cout << "b";

    // end

    And is it possible to give out a note for the whole time the key is pressed?


    Thank you very much!

    greetings,
    newatC

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    There's no standard C++ way to do this. You will need to do something operating-system specific like use WinAPI functions, for example.

    Of course, on a modern processor, timing is often iffy. Your process may lose CPU time to another process, leading it to detect the keydown or keyup as happening later than they really happened. So don't expect it to be very precise.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    16
    Hello,

    thank you for your answer, the problem is that i have written a
    morsetrainer and the console version is done. In this version the
    user must use two keys (dot and dash). But now i am writing
    a windows allication for this (GUI), and it is important that the
    output (dot or dash) is depending on how long the key was pressed,
    and that the user hear the morse-note.

    greetings,
    newatC

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    some resources for you:
    <ctime> FAQ*
    MSDN keyboard input
    MSDN PlaySound()
    MSDN DirectSound

    If you are new to Windows GUI programming, take a look at The Forger's Tutorial



    Here are some issues that come to mind:

    - There is no sound in standard C++. Of course, there is sound in the WinAPI library.

    - Standard C++ requires you to hit ENTER before it recognizes the input. Again, the WinAPI can see the keystroke before you hit ENTER. The Windows operating system will actually send a WM_KEYDOWN message to your Win32 GUI program when you press a key, and a WM_KEYUP message when you release it.

    - Like CAT said, Windows Multitasking makes timing inaccurate. I don’t know if this will be a problem. If you are not running any other applications, the timing errors should be under 50 milliseconds. Hopefully, that won’t cause a problem with the “dots” and “dashes”. You can get around the timing problems with a kernel mode (driver) program, but that’s not for beginners.

    - PlaySound() will play a sound of fixed duration. When you first press key, you don’t know if it’s a dot or a dash, so you would have to play a short sound in a loop while the key is held down. I’m not sure if this will sound like a continuous sound. I assume you can avoid this by using DirectSound (DirectX), but that’s not for beginners either.


    * The FAQ example uses #include <time.h> which is the C header. For C++, you should use change this to #include <ctime> .

Popular pages Recent additions subscribe to a feed