From 4b518ee6b97d0db39f5daf8a327f3515ce6e92a5 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 9 Nov 2013 11:12:55 +0000 Subject: [PATCH] Add support for function keys F1-F12 --- src/keys.cc | 28 ++++++++++++++++++++++++++++ src/keys.hh | 12 ++++++++++++ src/ncurses.cc | 6 ++++++ 3 files changed, 46 insertions(+) diff --git a/src/keys.cc b/src/keys.cc index 98909029..17789ccd 100644 --- a/src/keys.cc +++ b/src/keys.cc @@ -80,6 +80,29 @@ KeyList parse_keys(const String& str) pos = end_pos; continue; } + if ((keyname[0] == 'f' or keyname[0] == 'F') and + keyname.length() <= 3) + { + + int val = 0; + for (auto i = 1_byte; i < keyname.length(); ++i) + { + char c = keyname[i]; + if (c >= '0' and c <= '9') + val = val*10 + c - '0'; + else + { + val = -1; + break; + } + } + if (val >= 1 and val <= 12) + { + result.push_back(Key{ modifier, Key::F1 + (val - 1) }); + pos = end_pos; + continue; + } + } } } result.push_back({Key::Modifiers::None, Codepoint(str[pos])}); @@ -98,6 +121,11 @@ String key_to_str(Key key) named = true; res = it->first; } + else if (key.key >= Key::F1 and key.key < Key::F12) + { + named = true; + res = "F" + to_string((int)(Codepoint)key.key - (int)(Codepoint)Key::F1 + 1); + } else res = codepoint_to_str(key.key); diff --git a/src/keys.hh b/src/keys.hh index 05254f62..10cb3c5d 100644 --- a/src/keys.hh +++ b/src/keys.hh @@ -32,6 +32,18 @@ struct Key Home, End, BackTab, + F1, + F2, + F3, + F4, + F5, + F6, + F7, + F8, + F9, + F10, + F11, + F12, Invalid, }; diff --git a/src/ncurses.cc b/src/ncurses.cc index 4bd1b3b0..fc1560a5 100644 --- a/src/ncurses.cc +++ b/src/ncurses.cc @@ -354,6 +354,12 @@ Key NCursesUI::get_key() case KEY_BTAB: return Key::BackTab; } + for (int i = 0; i < 12; ++i) + { + if (c == KEY_F(i+1)) + return Key::F1 + i; + } + if (c >= 0 and c < 256) { ungetch(c);