input - OSx Keyboard presses C -


i'm coding experimental design , need able play sounds number of speakers/channels , have user press corresponding key when believe sound coming speaker/channel. (ie: participant thinks sound coming speaker 4 presses key 4). want able record how long takes between sound being played , time takes user press key.

as i'm playing sounds same application don't want lock application continually waiting user input. i'm guessing throw user input on thread what's best way achieve this? don't want user have press enter after each key press.

i'm using osx , c.

in synopsis form, code contain among other things 2 threads. run in secondary thread, ios tone initiator. in primary thread, elapsed timer, while loop includes key trap, , escape condition, whereby loop can exited when condition met.

some pseudo code: (using windows functions concept illustration)

int grunning = 1; //initiate tone in secondary thread  //initialize elapsed time keeper start while(grunning) {     //call key trap function here     //if key hit, set grunning == 0; } //get elapsed time here //kill tone , secondary thread  int keytrap(void) {     //write code here using getasynckeystate() check hits on relevant keys     return "any key hit" } 

the following functions used in actual implementation:

short getasynckeystate(int);   

and

time_t clock()   

getasynckeystate() commonly used in console applications allow monitoring, or responding user key strokes. if function succeeds, return value specifies whether key pressed since last call getasynckeystate, , whether key or down. if significant bit set, key down, , if least significant bit set, key pressed after previous call getasynckeystate. however, should not rely on last behavior; more information, see remarks.

for example, catch when 'k' or 'k' key has been hit, (you can code series of these "keyhandler" function multiple keys), call within in while loop:

    state = getasynckeystate('k');     state1 = getasynckeystate('k');      if ((0x80000000 & state) ||          (0x80000000 & state1))     {                  //do here     } 

clock() returns number of system clock cycles have occurred since program started executing. number of clock ticks can include time used other processes. convert number of clock cycles seconds, divide clocks_per_sec obtain approximation nearest millisecond.

note time.h in environment defines clocks_per_sec follows:

#if defined(_ni_unix_) || defined(_ni_sparc_) #define clocks_per_sec 1000000 #elif defined(_ni_mswin16_) || defined(_ni_mswin32_) || defined(_ni_mswin64_) #define clocks_per_sec 1000 #elif defined(_ni_mac_) #define clocks_per_sec 1   

so, mac, looks best resolution 1 second using clock()

another option, 1 giving millisecond resolution:
getlocaltime()

eg:

systemtime s; getlocaltime(&s);  swprintf_s(buff, l"[%02d:%02d:%02d:%d]\t", s.whour, s.wminute, s.wsecond, s.wmilliseconds); 

where systemtime defined:

typedef struct _systemtime {     word wyear;     word wmonth;     word wdayofweek;     word wday;     word whour;     word wminute;     word wsecond;     word wmilliseconds; } systemtime, *psystemtime, *lpsystemtime;   

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -