OSX键盘pressesÇ [英] OSx Keyboard presses C

查看:130
本文介绍了OSX键盘pressesÇ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编码实验设计,我需要能够从一些发言者/频道播放声音,然后让用户preSS对应的密钥时,他们认为,声音是从某个喇叭传出/渠道。 (即:参与者认为声音是从扬声器4来得那么presses键4)。我希望能够记录需要多长时间之间正在播放的声音和花费的时间为用户preSS的关键。

I'm coding an experimental design and I need to be able to play sounds from a number of speakers/channels and then have the user press a corresponding key when they believe the sound is coming from a certain speaker/channel. (ie: Participant thinks sound is coming from speaker 4 so presses key 4). I want to be able to record how long it takes between the sound being played and the time it takes for the user to press a key.

由于我打的声音从同一个应用程序,我真的不希望通过不断地等待用户输入来锁定应用程序。我猜我可能会引发用户的输入在另一个线程但什么是实现这一目标的最佳途径?我显然不希望用户有preSS每个键preSS后进入。

As I'm playing sounds from the same application I don't really want to lock up the application by continually waiting for user input. I'm guessing I could throw the user input on another thread but what's the best way to achieve this? I obviously don't want the user to have to press enter after each key press.

我使用OSX和C。

推荐答案

在大纲的形式后,您的code将包含除其他事项外2个线程。在辅助线程中运行,一个 iOS的语气引发 。在主线程中,定时器计时,while循环,其中包括一个关键的陷阱,逃生条件,由此,当条件满足可以退出循环。

In synopsis form, your code will contain among other things 2 threads. Run in a secondary thread, an iOS tone initiator. In the primary thread, an elapsed timer, a while loop that includes a key trap, and an escape condition, whereby the loop can be exited when condition met.

一些伪code:(使用说明的概念某些Windows功能)

Some pseudo code: (using some Windows functions for concept illustration)

int gRunning = 1;
//Initiate tone in a secondary thread 
//initialize elapsed time keeper to start
while(gRunning)
{
    //Call key trap function here
    //if Key Hit, set gRunning == 0;
}
//Get elapsed time here
//Kill tone and secondary thread

int KeyTrap(void)
{
    //Write code here using GetAsyncKeyState() to check hits on relevant keys
    return "any key hit"
}

以下功能可以在一个实际的实施中使用

The following functions could be used in an actual implementation:

short GetAsyncKeyState(int);  

time_t clock()  

GetAsyncKeyState() 在控制台应用程序通常用于允许监视,或响应用户按键。如果函数成功,返回值指定键是否是因为对GetAsyncKeyState最后一次通话pssed $ P $,关键是目前向上或向下。如果最显著位被设置,关键是向下,并且如果至少显著位被设置的,关键是previous调用GetAsyncKeyState后pssed $ P $。但是,你不应该依赖于这最后的行为;有关更多信息,请参见备注。

GetAsyncKeyState() is commonly used in console applications to allow monitoring, or responding to user key strokes. If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; for more information, see the Remarks.

例如,赶在k或K键已经被击中,(你可以codeA系列这些成一个keyHandler功能来获得多个键),然后在内部调用while循环:

For example, to catch when the 'k' or 'K' key has been hit, (you can code a series of these up into a "keyHandler" function to get multiple keys), then call within in a while loop:

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

时钟() 返回自程序开始执行已发生的系统时钟周期的数目。时钟滴答数可以包括其他进程使用的时间。要转换时钟周期来秒数,由CLOCKS_PER_SEC分获得近似到毫秒。

clock() Returns the number of system clock cycles that have occurred since the program started executing. The number of clock ticks can include time used by other processes. To convert the number of clock cycles to seconds, divide by CLOCKS_PER_SEC to obtain an approximation to the nearest millisecond.

注意 time.h中在我的环境定义CLOCKS_PER_SEC如下:

Note time.h in my environment defines CLOCKS_PER_SEC as 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  

所以,为Mac,它看起来像你的最佳分辨率将使用1秒时钟()

另一种选择,这一块给予毫秒分辨率:结果
GetLocalTime ()

Another option, this one giving millisecond resolution:
GetLocalTime()

例如:

SYSTEMTIME s;
GetLocalTime(&s); 
swprintf_s(buff, L"[%02d:%02d:%02d:%d]\t", s.wHour, s.wMinute, s.wSecond, s.wMilliseconds);

在哪里SYSTEMTIME定义:

Where SYSTEMTIME is defined:

typedef struct _SYSTEMTIME {
    WORD wYear;
    WORD wMonth;
    WORD wDayOfWeek;
    WORD wDay;
    WORD wHour;
    WORD wMinute;
    WORD wSecond;
    WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;  

这篇关于OSX键盘pressesÇ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆