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
diff --git a/webrtc/modules/audio_device/mac/audio_device_mac.cc b/webrtc/modules/audio_device/mac/audio_device_mac.cc
index 61b41cf..9da1880 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 @@
} \
} while(0)
+#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
+
enum
{
MaxNumberDevices = 64
@@ -153,7 +155,8 @@
_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 @@
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 7ad4fa0..a266223 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 @@
bool RenderWorkerThread();
private:
- bool KeyPressed() const;
+ bool KeyPressed();
private:
AudioDeviceBuffer* _ptrAudioBuffer;
@@ -377,6 +377,11 @@
int _captureBufSizeSamples;
int _renderBufSizeSamples;
+
+private:
+ // Typing detection
+ // 0x5c is key "9", after that comes function keys.
+ bool prev_key_state_[0x5d];
};
} // namespace webrtc