Skip to content

Commit dafd37c

Browse files
committed
trying to build keyboard
1 parent ea70f16 commit dafd37c

11 files changed

+66
-17
lines changed

XInputSimulator/XInputSimulator.pro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ macx {
2626
}
2727
unix:!macx{
2828
# linux only
29-
LIBS += -lX11
29+
LIBS += -lX11 -lXtst
3030
}
3131
win32 {
3232
# windows only

XInputSimulator/main.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,18 @@ int main()
5050
waitABit();
5151
sim.mouseScrollX(-10);
5252

53+
// char anA = 'a';
54+
// cout << "a: " << (int)anA << " " << sim.charToKeyCode(anA) << endl;
55+
// std::cout << std::endl;
56+
// waitABit();
57+
// sim.keyClick(sim.charToKeyCode(anA));
58+
// std::cout << std::endl;
59+
// waitABit();
60+
// sim.keySequence(" Simple sentence Here 123 ");
5361

54-
waitABit();
62+
63+
64+
//waitABit();
5565
return 0;
5666
}
5767

XInputSimulator/xinputsimulator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ void XInputSimulator::keyClick(int key)
7575
implementation->keyClick(key);
7676
}
7777

78-
void XInputSimulator::charToKeyCode(char key_char)
78+
int XInputSimulator::charToKeyCode(char key_char)
7979
{
80-
implementation->charToKeyCode(key_char);
80+
return implementation->charToKeyCode(key_char);
8181
}
8282

8383
void XInputSimulator::keySequence(const std::string &sequence)

XInputSimulator/xinputsimulator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class XInputSimulator
7272
void keyUp(int key);
7373
void keyClick(int key);
7474

75-
void charToKeyCode(char key_char);
75+
int charToKeyCode(char key_char);
7676
void keySequence(const std::string &sequence);
7777

7878
// mouse

XInputSimulator/xinputsimulatorimpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class XInputSimulatorImpl
3939
virtual void keyUp(int key) = 0;
4040
virtual void keyClick(int key) = 0;
4141

42-
virtual void charToKeyCode(char key_char) = 0;
42+
virtual int charToKeyCode(char key_char) = 0;
4343
virtual void keySequence(const std::string &sequence) = 0;
4444

4545
};

XInputSimulator/xinputsimulatorimpllinux.cpp

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,26 +141,63 @@ void XInputSimulatorImplLinux::mouseScrollY(int length)
141141

142142
void XInputSimulatorImplLinux::keyDown(int key)
143143
{
144-
throw NotImplementedException();
144+
//throw NotImplementedException();
145+
XTestFakeKeyEvent(display, key, True, 0);
146+
XFlush(display);
145147
}
146148

147149
void XInputSimulatorImplLinux::keyUp(int key)
148150
{
149-
throw NotImplementedException();
151+
//throw NotImplementedException();
152+
XTestFakeKeyEvent(display, key, False, 0);
153+
XFlush(display);
150154
}
151155

152156
void XInputSimulatorImplLinux::keyClick(int key)
153157
{
154-
throw NotImplementedException();
158+
std::cout << "key click: " << key << std::endl;
159+
//throw NotImplementedException();
160+
this->keyDown(key);
161+
this->keyUp(key);
155162
}
156163

157-
void XInputSimulatorImplLinux::charToKeyCode(char key_char)
164+
int XInputSimulatorImplLinux::charToKeyCode(char key_char)
158165
{
159-
throw NotImplementedException();
166+
std::cout << "cchar: " << (int)key_char << std::endl;
167+
168+
//throw NotImplementedException();
169+
170+
// KeySym sym = XStringToKeysym(&key_char);
171+
172+
// std::cout << "sym: " << sym << std::endl;
173+
174+
int keyCode = XKeysymToKeycode(display, key_char);
175+
std::cout << "ccode: " << keyCode << std::endl;
176+
177+
return keyCode;
160178
}
161179
void XInputSimulatorImplLinux::keySequence(const std::string &sequence)
162180
{
163-
throw NotImplementedException();
181+
//throw NotImplementedException();
182+
183+
std::cout << "key seq: " << sequence << std::endl;
184+
185+
//c++11 training
186+
// for(auto it = sequence.begin(); it != sequence.end(); ++it) {
187+
// std::cout << "key org: " << (int)(*it) << std::endl;
188+
// int keyCode = this->charToKeyCode(*it);
189+
// std::cout << "key code: " << keyCode << std::endl;
190+
// this->keyClick(keyCode);
191+
// }
192+
//c++11 better
193+
for(const char c : sequence) {
194+
std::cout << "cahr: " << c << std::endl;
195+
int keyCode = this->charToKeyCode(c);
196+
std::cout << "key code: " << keyCode << std::endl;
197+
this->keyClick(keyCode);
198+
std::cout << std::endl;
199+
}
200+
164201
}
165202

166203

XInputSimulator/xinputsimulatorimpllinux.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
#include <X11/Xlib.h>
2424
#include <X11/Xutil.h>
25+
#include <X11/keysym.h>
26+
#include <X11/extensions/XTest.h>
2527

2628
#include "xinputsimulatorimpl.h"
2729

@@ -50,7 +52,7 @@ class XInputSimulatorImplLinux : public XInputSimulatorImpl
5052
virtual void keyUp(int key) override;
5153
virtual void keyClick(int key) override;
5254

53-
virtual void charToKeyCode(char key_char) override;
55+
virtual int charToKeyCode(char key_char) override;
5456
virtual void keySequence(const std::string &sequence) override;
5557

5658
};

XInputSimulator/xinputsimulatorimplmacos.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ void XInputSimulatorImplMacOs::keyClick(int key)
192192
throw NotImplementedException();
193193
}
194194

195-
void XInputSimulatorImplMacOs::charToKeyCode(char key_char)
195+
int XInputSimulatorImplMacOs::charToKeyCode(char key_char)
196196
{
197197
throw NotImplementedException();
198198
}

XInputSimulator/xinputsimulatorimplmacos.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class XInputSimulatorImplMacOs : public XInputSimulatorImpl
4747
virtual void keyUp(int key) override;
4848
virtual void keyClick(int key) override;
4949

50-
virtual void charToKeyCode(char key_char) override;
50+
virtual int charToKeyCode(char key_char) override;
5151
virtual void keySequence(const std::string &sequence) override;
5252
};
5353

XInputSimulator/xinputsimulatorimplwin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ void XInputSimulatorImplWin::keyClick(int key)
149149
throw NotImplementedException();
150150
}
151151

152-
void XInputSimulatorImplWin::charToKeyCode(char key_char)
152+
int XInputSimulatorImplWin::charToKeyCode(char key_char)
153153
{
154154
throw NotImplementedException();
155155
}

0 commit comments

Comments
 (0)