Fix OSX keydown detection. I noticed that the OSX implementation differs from Linux and Windows, and it will trigger continuously for a key that is pressed down. It would totally make sense to change this to a callback driven model, but that's a bigger change.

I need to test this before committing...

R=tommi@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/1996004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@4550 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
niklas.enbom@webrtc.org 2013-08-15 14:19:12 +00:00
parent c92781737c
commit cc9238e385
2 changed files with 23 additions and 9 deletions
webrtc/modules/audio_device/mac

@ -56,6 +56,8 @@ namespace webrtc
} \ } \
} while(0) } while(0)
#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
enum enum
{ {
MaxNumberDevices = 64 MaxNumberDevices = 64
@ -153,7 +155,8 @@ AudioDeviceMac::AudioDeviceMac(const int32_t id) :
_paCaptureBuffer(NULL), _paCaptureBuffer(NULL),
_paRenderBuffer(NULL), _paRenderBuffer(NULL),
_captureBufSizeSamples(0), _captureBufSizeSamples(0),
_renderBufSizeSamples(0) _renderBufSizeSamples(0),
prev_key_state_()
{ {
WEBRTC_TRACE(kTraceMemory, kTraceAudioDevice, id, WEBRTC_TRACE(kTraceMemory, kTraceAudioDevice, id,
"%s created", __FUNCTION__); "%s created", __FUNCTION__);
@ -3259,14 +3262,20 @@ bool AudioDeviceMac::CaptureWorkerThread()
return true; return true;
} }
bool AudioDeviceMac::KeyPressed() const{ bool AudioDeviceMac::KeyPressed() {
bool key_down = false; bool key_down = false;
// loop through all Mac virtual key constant values // Loop through all Mac virtual key constant values.
for (int key_index = 0; key_index <= 0x5C; key_index++) { for (unsigned int key_index = 0;
key_down |= CGEventSourceKeyState(kCGEventSourceStateHIDSystemState, key_index < ARRAY_SIZE(prev_key_state_);
key_index); ++key_index) {
bool keyState = CGEventSourceKeyState(
kCGEventSourceStateHIDSystemState,
key_index);
// A false -> true change in keymap means a key is pressed.
key_down |= (keyState && !prev_key_state_[key_index]);
// Save current state.
prev_key_state_[key_index] = keyState;
} }
return(key_down); return key_down;
} }
} // namespace webrtc } // namespace webrtc

@ -285,7 +285,7 @@ private:
bool RenderWorkerThread(); bool RenderWorkerThread();
private: private:
bool KeyPressed() const; bool KeyPressed();
private: private:
AudioDeviceBuffer* _ptrAudioBuffer; AudioDeviceBuffer* _ptrAudioBuffer;
@ -377,6 +377,11 @@ private:
int _captureBufSizeSamples; int _captureBufSizeSamples;
int _renderBufSizeSamples; int _renderBufSizeSamples;
private:
// Typing detection
// 0x5c is key "9", after that comes function keys.
bool prev_key_state_[0x5d];
}; };
} // namespace webrtc } // namespace webrtc