Add experimental new line support for windows

This commit is contained in:
Federico Terzi 2019-09-06 10:21:33 +02:00
parent e604749433
commit 072d883583
5 changed files with 44 additions and 2 deletions

View File

@ -223,10 +223,28 @@ void delete_string(int32_t count) {
input.ki.dwFlags = 0; // 0 for key press
vec.push_back(input);
// Release the "A" key
input.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
vec.push_back(input);
}
SendInput(vec.size(), vec.data(), sizeof(INPUT));
}
void send_vkey(int32_t vk) {
std::vector<INPUT> vec;
INPUT input = { 0 };
input.type = INPUT_KEYBOARD;
input.ki.wScan = 0;
input.ki.time = 0;
input.ki.dwExtraInfo = 0;
input.ki.wVk = vk;
input.ki.dwFlags = 0; // 0 for key press
vec.push_back(input);
input.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
vec.push_back(input);
SendInput(vec.size(), vec.data(), sizeof(INPUT));
}

View File

@ -34,6 +34,11 @@ extern "C" void eventloop();
*/
extern "C" void send_string(const wchar_t * string);
/*
* Send the given Virtual Key press
*/
extern "C" void send_vkey(int32_t vk);
/*
* Send the backspace keypress, *count* times.
*/

View File

@ -16,6 +16,16 @@ impl <S> Engine<S> where S: KeyboardSender{
impl <S> MatchReceiver for Engine<S> where S: KeyboardSender{
fn on_match(&self, m: &Match) {
self.sender.delete_string(m.trigger.len() as i32);
self.sender.send_string(m.replace.as_str());
// To handle newlines, substitute each "\n" char with an Enter key press.
let splits = m.replace.lines();
for (i, split) in splits.enumerate() {
if i > 0 {
self.sender.send_enter();
}
self.sender.send_string(split);
}
}
}

View File

@ -38,6 +38,7 @@ pub enum KeyEvent {
pub trait KeyboardSender {
fn send_string(&self, s: &str);
fn send_enter(&self);
fn delete_string(&self, count: i32);
}

View File

@ -40,6 +40,13 @@ impl super::KeyboardSender for WindowsKeyboardSender {
}
fn send_enter(&self) {
unsafe {
// Send the VK_RETURN key press
send_vkey(0x0D);
}
}
fn delete_string(&self, count: i32) {
unsafe {
delete_string(count)
@ -87,5 +94,6 @@ extern {
fn initialize_window();
fn eventloop();
fn send_string(string: *const u16);
fn send_vkey(vk: i32);
fn delete_string(count: i32);
}