Cleanup Key handling, use symbolic names for function keys
This commit is contained in:
parent
287628ec19
commit
499bb77491
|
@ -59,15 +59,16 @@ public:
|
||||||
|
|
||||||
void on_key(const Key& key, Context& context) override
|
void on_key(const Key& key, Context& context) override
|
||||||
{
|
{
|
||||||
if (key == Key(Key::Modifiers::Control, 'n') or
|
if (key == Key::Down or
|
||||||
key == Key(Key::Modifiers::Control, 'i') or
|
key == Key(Key::Modifiers::Control, 'n') or
|
||||||
key == Key(Key::Modifiers::None, 'j'))
|
key == Key(Key::Modifiers::None, 'j'))
|
||||||
{
|
{
|
||||||
if (++m_selected >= m_choice_count)
|
if (++m_selected >= m_choice_count)
|
||||||
m_selected = 0;
|
m_selected = 0;
|
||||||
m_client.menu_select(m_selected);
|
m_client.menu_select(m_selected);
|
||||||
}
|
}
|
||||||
if (key == Key(Key::Modifiers::Control, 'p') or
|
if (key == Key::Up or
|
||||||
|
key == Key(Key::Modifiers::Control, 'p') or
|
||||||
key == Key(Key::Modifiers::None, 'k'))
|
key == Key(Key::Modifiers::None, 'k'))
|
||||||
{
|
{
|
||||||
if (--m_selected < 0)
|
if (--m_selected < 0)
|
||||||
|
@ -82,7 +83,7 @@ public:
|
||||||
m_client.reset_normal_mode();
|
m_client.reset_normal_mode();
|
||||||
callback(selected, context);
|
callback(selected, context);
|
||||||
}
|
}
|
||||||
if (key == Key(Key::Modifiers::None, 27))
|
if (key == Key::Escape)
|
||||||
{
|
{
|
||||||
m_client.reset_normal_mode();
|
m_client.reset_normal_mode();
|
||||||
}
|
}
|
||||||
|
@ -122,7 +123,7 @@ public:
|
||||||
void on_key(const Key& key, Context& context) override
|
void on_key(const Key& key, Context& context) override
|
||||||
{
|
{
|
||||||
std::vector<String>& history = ms_history[m_prompt];
|
std::vector<String>& history = ms_history[m_prompt];
|
||||||
if (key == Key(Key::Modifiers::Control, 'm')) // enter
|
if (key == Key{Key::Modifiers::Control, 'm'}) // enter
|
||||||
{
|
{
|
||||||
std::vector<String>::iterator it;
|
std::vector<String>::iterator it;
|
||||||
while ((it = find(history, m_result)) != history.end())
|
while ((it = find(history, m_result)) != history.end())
|
||||||
|
@ -137,14 +138,14 @@ public:
|
||||||
callback(result, context);
|
callback(result, context);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else if (key == Key(Key::Modifiers::None, 27))
|
else if (key == Key::Escape)
|
||||||
{
|
{
|
||||||
m_client.print_status("");
|
m_client.print_status("");
|
||||||
m_client.reset_normal_mode();
|
m_client.reset_normal_mode();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else if (key == Key(Key::Modifiers::Control, 'p') or // previous
|
else if (key == Key::Up or
|
||||||
key == Key(Key::Modifiers::Control, 'c'))
|
key == Key{Key::Modifiers::Control, 'p'})
|
||||||
{
|
{
|
||||||
if (m_history_it != history.begin())
|
if (m_history_it != history.begin())
|
||||||
{
|
{
|
||||||
|
@ -166,8 +167,8 @@ public:
|
||||||
} while (it != history.begin());
|
} while (it != history.begin());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (key == Key(Key::Modifiers::Control, 'n') or // next
|
else if (key == Key::Down or // next
|
||||||
key == Key(Key::Modifiers::Control, 'b'))
|
key == Key{Key::Modifiers::Control, 'n'})
|
||||||
{
|
{
|
||||||
if (m_history_it != history.end())
|
if (m_history_it != history.end())
|
||||||
{
|
{
|
||||||
|
@ -185,17 +186,19 @@ public:
|
||||||
m_cursor_pos = m_result.length();
|
m_cursor_pos = m_result.length();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (key == Key(Key::Modifiers::Control, 'd'))
|
else if (key == Key::Left or
|
||||||
|
key == Key{Key::Modifiers::Control, 'b'})
|
||||||
{
|
{
|
||||||
if (m_cursor_pos > 0)
|
if (m_cursor_pos > 0)
|
||||||
--m_cursor_pos;
|
--m_cursor_pos;
|
||||||
}
|
}
|
||||||
else if (key == Key(Key::Modifiers::Control, 'e'))
|
else if (key == Key::Right or
|
||||||
|
key == Key{Key::Modifiers::Control, 'f'})
|
||||||
{
|
{
|
||||||
if (m_cursor_pos < m_result.length())
|
if (m_cursor_pos < m_result.length())
|
||||||
++m_cursor_pos;
|
++m_cursor_pos;
|
||||||
}
|
}
|
||||||
else if (key == Key(Key::Modifiers::Control, 'g')) // backspace
|
else if (key == Key::Backspace)
|
||||||
{
|
{
|
||||||
if (m_cursor_pos != 0)
|
if (m_cursor_pos != 0)
|
||||||
{
|
{
|
||||||
|
@ -322,9 +325,12 @@ public:
|
||||||
case Key::Modifiers::None:
|
case Key::Modifiers::None:
|
||||||
switch (key.key)
|
switch (key.key)
|
||||||
{
|
{
|
||||||
case 27:
|
case Key::Escape:
|
||||||
m_client.reset_normal_mode();
|
m_client.reset_normal_mode();
|
||||||
return;
|
return;
|
||||||
|
case Key::Backspace:
|
||||||
|
m_inserter.erase();
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
m_inserter.insert(String() + key.key);
|
m_inserter.insert(String() + key.key);
|
||||||
}
|
}
|
||||||
|
@ -347,9 +353,6 @@ public:
|
||||||
case 'e':
|
case 'e':
|
||||||
m_inserter.move_cursors({0, 1});
|
m_inserter.move_cursors({0, 1});
|
||||||
break;
|
break;
|
||||||
case 'g':
|
|
||||||
m_inserter.erase();
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
12
src/keys.hh
12
src/keys.hh
|
@ -16,6 +16,15 @@ struct Key
|
||||||
Alt = 2,
|
Alt = 2,
|
||||||
ControlAlt = 3
|
ControlAlt = 3
|
||||||
};
|
};
|
||||||
|
enum NamedKeys : Character
|
||||||
|
{
|
||||||
|
Backspace = 256,
|
||||||
|
Escape,
|
||||||
|
Up,
|
||||||
|
Down,
|
||||||
|
Left,
|
||||||
|
Right,
|
||||||
|
};
|
||||||
|
|
||||||
Modifiers modifiers;
|
Modifiers modifiers;
|
||||||
Character key;
|
Character key;
|
||||||
|
@ -23,6 +32,9 @@ struct Key
|
||||||
constexpr Key(Modifiers modifiers, Character key)
|
constexpr Key(Modifiers modifiers, Character key)
|
||||||
: modifiers(modifiers), key(key) {}
|
: modifiers(modifiers), key(key) {}
|
||||||
|
|
||||||
|
constexpr Key(Character key)
|
||||||
|
: modifiers(Modifiers::None), key(key) {}
|
||||||
|
|
||||||
constexpr bool operator==(const Key& other) const
|
constexpr bool operator==(const Key& other) const
|
||||||
{ return modifiers == other.modifiers and key == other.key; }
|
{ return modifiers == other.modifiers and key == other.key; }
|
||||||
};
|
};
|
||||||
|
|
|
@ -164,26 +164,31 @@ void NCursesClient::draw_window(Window& window)
|
||||||
|
|
||||||
Key NCursesClient::get_key()
|
Key NCursesClient::get_key()
|
||||||
{
|
{
|
||||||
char c = getch();
|
const int c = getch();
|
||||||
|
|
||||||
Key::Modifiers modifiers = Key::Modifiers::None;
|
|
||||||
if (c > 0 and c < 27)
|
if (c > 0 and c < 27)
|
||||||
{
|
{
|
||||||
modifiers = Key::Modifiers::Control;
|
return {Key::Modifiers::Control, c - 1 + 'a'};
|
||||||
c = c - 1 + 'a';
|
|
||||||
}
|
}
|
||||||
else if (c == 27)
|
else if (c == 27)
|
||||||
{
|
{
|
||||||
timeout(0);
|
timeout(0);
|
||||||
char new_c = getch();
|
const int new_c = getch();
|
||||||
timeout(-1);
|
timeout(-1);
|
||||||
if (new_c != ERR)
|
if (new_c != ERR)
|
||||||
{
|
return {Key::Modifiers::Alt, new_c};
|
||||||
c = new_c;
|
else
|
||||||
modifiers = Key::Modifiers::Alt;
|
return Key::Escape;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return Key(modifiers, c);
|
else switch (c)
|
||||||
|
{
|
||||||
|
case KEY_BACKSPACE: return Key::Backspace;
|
||||||
|
case KEY_UP: return Key::Up;
|
||||||
|
case KEY_DOWN: return Key::Down;
|
||||||
|
case KEY_LEFT: return Key::Left;
|
||||||
|
case KEY_RIGHT: return Key::Right;
|
||||||
|
}
|
||||||
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NCursesClient::print_status(const String& status, CharCount cursor_pos)
|
void NCursesClient::print_status(const String& status, CharCount cursor_pos)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user