Skip to main content

Automated Keyboard Typing in Multiple Languages & Keeping the Screen Awake

· 6 min read
Anand Raja
Senior Software Engineer

This document demonstrates how to simulate typing the string "welcome to evoke " repeatedly in the currently focused window using PowerShell, Node.js, Python, C, C++, and Ruby. Each section explains the code, how to save it, how to execute it, and how to stop the running program.


1. PowerShell

Code:

Add-Type -AssemblyName System.Windows.Forms

try {
while ($true) {
# Simulate typing "welcome to evoke "
$keys = "welcome to evoke "
foreach ($key in $keys.ToCharArray()) {
[System.Windows.Forms.SendKeys]::SendWait($key)
Start-Sleep -Milliseconds 100
}
Start-Sleep -Seconds 30
}
} catch {
Write-Host "Script terminated due to an error: $_"
}

How to Save:
Save the code in a file named type-welcome.ps1.

How to Run:
Open PowerShell, navigate to the file location, and run:

powershell -ExecutionPolicy Bypass -File ./type-welcome.ps1

How to Stop:
Press Ctrl + C in the PowerShell window.


2. Node.js

Code:

const robot = require('robotjs');

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

async function main() {
try {
while (true) {
const keys = "welcome to evoke ";
for (const key of keys) {
robot.typeString(key);
await sleep(100); // 100 milliseconds
}
await sleep(30000); // 30 seconds
}
} catch (err) {
console.log(`Script terminated due to an error: ${err}`);
}
}

main();

How to Save:
Save the code in a file named type-welcome.js.

How to Run:

  1. Install robotjs:
    yarn add robotjs
  2. Run the script:
    node type-welcome.js

How to Stop:
Press Ctrl + C in the terminal.


3. Python

Code:

import pyautogui
import time

try:
while True:
keys = "welcome to evoke "
for key in keys:
pyautogui.typewrite(key)
time.sleep(0.1) # 100 milliseconds
time.sleep(30) # 30 seconds
except Exception as e:
print(f"Script terminated due to an error: {e}")

How to Save:
Save the code in a file named type_welcome.py.

How to Run:

  1. Install pyautogui:
    pip install pyautogui
  2. Run the script:
    python type_welcome.py

How to Stop:
Press Ctrl + C in the terminal.


4. C

Windows Version

Code:

#include <windows.h>
#include <stdio.h>
#include <string.h>

void typeString(const char* str) {
for (int i = 0; i < strlen(str); i++) {
SHORT vk = VkKeyScan(str[i]);
keybd_event((BYTE)vk, 0, 0, 0);
keybd_event((BYTE)vk, 0, KEYEVENTF_KEYUP, 0);
Sleep(100); // 100 milliseconds
}
}

int main() {
while (1) {
typeString("welcome to evoke ");
Sleep(30000); // 30 seconds
}
return 0;
}

How to Save:
Save the code in a file named type_welcome.c.

How to Run:

  1. Compile with a Windows C compiler (e.g., MinGW or Visual Studio).
  2. Run the resulting executable.

How to Stop:
Close the command prompt window or press Ctrl + C if running in a terminal.

Cross-Platform Version (with libuiohook)

Code:

// Requires libuiohook: https://github.com/kwhat/libuiohook
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <uiohook.h>

void typeString(const char* str) {
for (size_t i = 0; i < strlen(str); i++) {
uiohook_post_keyboard_event(str[i], 1); // key press
uiohook_post_keyboard_event(str[i], 0); // key release
usleep(100000); // 100 ms
}
}

int main() {
while (1) {
typeString("welcome to evoke ");
sleep(30);
}
return 0;
}

How to Save:
Save as type_welcome.c.

How to Run:

  1. Install libuiohook for your OS.
  2. Compile:
    gcc type_welcome.c -luiohook -o type_welcome
  3. Run:
    ./type_welcome

How to Stop:
Press Ctrl + C in the terminal.


5. C++

Windows Version

Code:

#include <windows.h>
#include <string>

void typeString(const std::string& str) {
for (char ch : str) {
SHORT vk = VkKeyScan(ch);
INPUT input = {0};
input.type = INPUT_KEYBOARD;
input.ki.wVk = vk;
SendInput(1, &input, sizeof(INPUT));
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(INPUT));
Sleep(100); // 100 milliseconds
}
}

int main() {
while (true) {
typeString("welcome to evoke ");
Sleep(30000); // 30 seconds
}
return 0;
}

How to Save:
Save the code in a file named type_welcome.cpp.

How to Run:

  1. Compile with a Windows C++ compiler (e.g., MinGW or Visual Studio).
  2. Run the resulting executable.

How to Stop:
Close the command prompt window or press Ctrl + C if running in a terminal.

Cross-Platform Version (with libuiohook)

Code:

// Requires libuiohook: https://github.com/kwhat/libuiohook
#include <iostream>
#include <thread>
#include <chrono>
#include <cstring>
#include <uiohook.h>

void typeString(const std::string& str) {
for (char ch : str) {
uiohook_post_keyboard_event(ch, 1); // key press
uiohook_post_keyboard_event(ch, 0); // key release
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}

int main() {
while (true) {
typeString("welcome to evoke ");
std::this_thread::sleep_for(std::chrono::seconds(30));
}
return 0;
}

How to Save:
Save as type_welcome.cpp.

How to Run:

  1. Install libuiohook.
  2. Compile:
    g++ type_welcome.cpp -luiohook -o type_welcome
  3. Run:
    ./type_welcome

How to Stop:
Press Ctrl + C in the terminal.


6. Ruby

Windows Version

Code:

require 'win32/api'

keybd_event = Win32::API.new('keybd_event', 'LLLL', 'V', 'user32')
vk_key_scan = Win32::API.new('VkKeyScan', 'I', 'I', 'user32')

def type_string(str, keybd_event, vk_key_scan)
str.each_byte do |ch|
vk = vk_key_scan.call(ch)
keybd_event.call(vk, 0, 0, 0)
keybd_event.call(vk, 0, 2, 0) # KEYEVENTF_KEYUP = 2
sleep 0.1
end
end

loop do
type_string("welcome to evoke ", keybd_event, vk_key_scan)
sleep 30
end

How to Save:
Save the code in a file named type_welcome.rb.

How to Run:

  1. Install the gem:
    gem install win32-api
  2. Run the script:
    ruby type_welcome.rb

How to Stop:
Press Ctrl + C in the terminal.

Cross-Platform Version

Option 1: Using xdotool (Linux/macOS)

Code:

loop do
system("xdotool type 'welcome to evoke '")
sleep 30
end

How to Save:
Save as type_welcome_linux.rb.

How to Run:

  1. Install xdotool (sudo apt install xdotool or brew install xdotool).
  2. Run:
    ruby type_welcome_linux.rb

Option 2: Using rb-keysim (Cross-platform Ruby Gem)

Code:

require 'rb-keysim'

keyboard = Keysim::Keyboard.new

loop do
keyboard.type('welcome to evoke ')
sleep 30
end

How to Save:
Save as type_welcome.rb.

How to Run:

  1. Install the gem:
    gem install rb-keysim
  2. Run:
    ruby type_welcome.rb

How to Stop:
Press Ctrl + C in the terminal.


Important Notes

  • All scripts will type "welcome to evoke " into the currently focused window every 30 seconds.
  • For Windows-specific code (C, C++, Ruby), run on a Windows system.
  • For cross-platform versions, ensure the required libraries are installed for your OS.
  • For PowerShell, Node.js, and Python, make sure the required libraries are installed.
  • Always run these scripts with the window you want to type into focused.
  • To stop the scripts, use Ctrl + C in the terminal or close the running process.