From cc9238e385bca5bd1ebe95d38bb49798cd9d567a Mon Sep 17 00:00:00 2001 From: "niklas.enbom@webrtc.org" Date: Thu, 15 Aug 2013 14:19:12 +0000 Subject: [PATCH] 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 --- .../audio_device/mac/audio_device_mac.cc | 25 +++++++++++++------ .../audio_device/mac/audio_device_mac.h | 7 +++++- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/webrtc/modules/audio_device/mac/audio_device_mac.cc b/webrtc/modules/audio_device/mac/audio_device_mac.cc index 61b41cffe..9da188013 100644 --- a/webrtc/modules/audio_device/mac/audio_device_mac.cc +++ b/webrtc/modules/audio_device/mac/audio_device_mac.cc @@ -56,6 +56,8 @@ namespace webrtc } \ } while(0) +#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) + enum { MaxNumberDevices = 64 @@ -153,7 +155,8 @@ AudioDeviceMac::AudioDeviceMac(const int32_t id) : _paCaptureBuffer(NULL), _paRenderBuffer(NULL), _captureBufSizeSamples(0), - _renderBufSizeSamples(0) + _renderBufSizeSamples(0), + prev_key_state_() { WEBRTC_TRACE(kTraceMemory, kTraceAudioDevice, id, "%s created", __FUNCTION__); @@ -3259,14 +3262,20 @@ bool AudioDeviceMac::CaptureWorkerThread() return true; } -bool AudioDeviceMac::KeyPressed() const{ - +bool AudioDeviceMac::KeyPressed() { bool key_down = false; - // loop through all Mac virtual key constant values - for (int key_index = 0; key_index <= 0x5C; key_index++) { - key_down |= CGEventSourceKeyState(kCGEventSourceStateHIDSystemState, - key_index); + // Loop through all Mac virtual key constant values. + for (unsigned int key_index = 0; + key_index < ARRAY_SIZE(prev_key_state_); + ++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 diff --git a/webrtc/modules/audio_device/mac/audio_device_mac.h b/webrtc/modules/audio_device/mac/audio_device_mac.h index 7ad4fa063..a26622392 100644 --- a/webrtc/modules/audio_device/mac/audio_device_mac.h +++ b/webrtc/modules/audio_device/mac/audio_device_mac.h @@ -285,7 +285,7 @@ private: bool RenderWorkerThread(); private: - bool KeyPressed() const; + bool KeyPressed(); private: AudioDeviceBuffer* _ptrAudioBuffer; @@ -377,6 +377,11 @@ private: int _captureBufSizeSamples; int _renderBufSizeSamples; + +private: + // Typing detection + // 0x5c is key "9", after that comes function keys. + bool prev_key_state_[0x5d]; }; } // namespace webrtc