Index: examples/media_test/keystroke.cc |
diff --git a/examples/media_test/keystroke.cc b/examples/media_test/keystroke.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..15ba3ae560e4bf69fb5c6d48d1f59c31fddf10d6 |
--- /dev/null |
+++ b/examples/media_test/keystroke.cc |
@@ -0,0 +1,44 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <fcntl.h> |
+#include <stdio.h> |
+#include <unistd.h> |
+ |
+#include <iostream> |
+ |
+#include "examples/media_test/keystroke.h" |
+ |
+namespace examples { |
+ |
+bool eat_newline = false; |
+bool upped_already = false; |
johngro
2016/03/21 22:10:46
This should either be hidden as private static mem
dalesat
2016/03/21 23:58:34
Done.
|
+ |
+char Keystroke(void) { |
+ const char *kUp = "\033[A"; |
+ |
+ fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK); |
johngro
2016/03/21 22:10:46
you can say STDIN_FILENO instead of 0 here. You w
dalesat
2016/03/21 23:58:34
Done.
|
+ char buf[1]; |
+ if (read(0, buf, 1) > 0) { |
+ if (!upped_already) { |
johngro
2016/03/21 22:10:46
so, this is terminal specific and is probably not
dalesat
2016/03/21 23:58:34
Acknowledged.
|
+ std::cerr << kUp << std::flush; |
johngro
2016/03/21 22:10:46
Why cerr and not cout?
dalesat
2016/03/21 23:58:34
Force of habit. Fixed.
|
+ upped_already = true; |
+ } |
+ |
+ if (buf[0] == '\n') { |
+ upped_already = false; |
+ if (eat_newline) { |
+ eat_newline = false; |
+ } else { |
+ return buf[0]; |
+ } |
+ } else { |
+ eat_newline = true; |
+ return buf[0]; |
+ } |
+ } |
+ return 0; |
+} |
+ |
+} // namespace examples |