-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeypressInteractionReader.cpp
80 lines (73 loc) · 2.47 KB
/
KeypressInteractionReader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// KeypressInteractionReader.cpp
//
// ICS 45C Spring 2022
// Project #4: People Just Want to Play with Words
//
// Implementation of the KeypressInteractionReader
//
// YOU WILL NEED TO IMPLEMENT SOME THINGS HERE
#include "KeypressInteractionReader.hpp"
#include "CursorRight.hpp"
#include "CursorLeft.hpp"
#include "CursorUp.hpp"
#include "CursorDown.hpp"
#include "CursorHome.hpp"
#include "CursorEnd.hpp"
#include "InsertChar.hpp"
#include "NewLine.hpp"
#include "DeleteLine.hpp"
#include "Backspace.hpp"
// You will need to update this member function to watch for the right
// keypresses and build the right kinds of Interactions as a result.
//
// The reason you'll need to implement things here is that you'll need
// to create Command objects, which you'll be declaring and defining
// yourself; they haven't been provided already.
//
// I've added handling for "quit" here, but you'll need to add the others.
Interaction KeypressInteractionReader::nextInteraction()
{
while (true)
{
Keypress keypress = keypressReader.nextKeypress();
if (keypress.ctrl())
{
// The user pressed a Ctrl key (e.g., Ctrl+X); react accordingly
switch (keypress.code())
{
case 'X':
return Interaction::quit();
case 'O':
return Interaction::command(new CursorRight());
case 'U':
return Interaction::command(new CursorLeft());
case 'I':
return Interaction::command(new CursorUp());
case 'K':
return Interaction::command(new CursorDown());
case 'Y':
return Interaction::command(new CursorHome());
case 'P':
return Interaction::command(new CursorEnd());
case 'J':
return Interaction::command(new NewLine());
case 'M':
return Interaction::command(new NewLine());
case 'D':
return Interaction::command(new DeleteLine());
case 'H':
return Interaction::command(new Backspace());
case 'Z':
return Interaction::undo();
case 'A':
return Interaction::redo();
}
}
else
{
// The user pressed a normal key (e.g., 'h') without holding
// down Ctrl; react accordingly
return Interaction::command(new InsertChar(keypress.code()));
}
}
}