/
ConsoleListener.h
executable file
·105 lines (78 loc) · 1.57 KB
/
ConsoleListener.h
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#pragma once
#include "ofMain.h"
class SSHKeyListenerEventData
{
public:
SSHKeyListenerEventData(char character_, void* listener_)
{
character = character_;
listener = listener_;
}
void* listener;
char character;
};
class SSHKeyListener
{
public:
virtual void onCharacterReceived(SSHKeyListenerEventData& e) = 0;
};
class ConsoleListener: public Poco::Runnable
{
public:
SSHKeyListener* listener;
Poco::Thread thread;
ConsoleListener()
{
listener = NULL;
}
~ConsoleListener()
{
thread.tryJoin(50);
ofLogVerbose() << "~ConsoleListener END";
}
void setup(SSHKeyListener* listener_)
{
listener = listener_;
//thread.setOSPriority(Poco::Thread::getMinOSPriority());
thread.start(*this);
}
void run()
{
while (thread.isRunning())
{
if (listener != NULL)
{
char buffer[10];
if (fgets(buffer, 10 , stdin) != NULL)
{
SSHKeyListenerEventData eventData(buffer[0], (void *)this);
listener->onCharacterReceived(eventData);
}
}
}
}
};
#if 0
USAGE:
1.add to testApp.h
#include "ConsoleListener.h"
//extend testApp
class testApp : public ofBaseApp, public SSHKeyListener
2. add required callback definition and instance
void onCharacterReceived(SSHKeyListenerEventData& e);
ConsoleListener consoleListener;
3. add to testApp.cpp
void testApp::onCharacterReceived(SSHKeyListenerEventData& e)
{
keyPressed((int)e.character);
}
add in testApp::setup()
consoleListener.setup(this);
4. and later
void testApp::keyPressed (int key){
if (key == 'e')
{
ofLogVerbose() << "e pressed!";
}
}
#endif