Skip to content

Instantly share code, notes, and snippets.

@univrsal
Last active July 3, 2016 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save univrsal/cd1a85b27151d2f0814ffb501e4ae143 to your computer and use it in GitHub Desktop.
Save univrsal/cd1a85b27151d2f0814ffb501e4ae143 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace RussianTextGen
{
class Program
{
static int m_sendKeyCode { get; set; }
static Keys m_sendKeybind { get; set; }
static int m_maxWords = 7;
static int m_minWords = 3;
static int m_minWordLength = 3;
static int m_maxWordLength = 8;
static bool m_hasSent = false;
static int m_refresh = 100;
static Random rnd = new Random();
/* Russian characters */
static string m_russianLC = "абвгдеёъжрщзийклмнопчсфтушхцыьэюя";
static string m_russianUC = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧЬШЩЭЪЫЯЮ";
static string[] m_keyWords = { "Сyka", "Ыyат", "vodka", "da", "niet" };
static string m_marks = ".?!,";
static string sentence = "";
static string tempWord = "";
static NotifyIcon m_tray = new NotifyIcon();
// Keyboard Hook
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private static LowLevelKeyboardProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
static bool isHidden = false;
private static IntPtr SetHook(LowLevelKeyboardProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_KEYBOARD_LL, proc,GetModuleHandle(curModule.ModuleName), 0);
}
}
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
m_sendKeyCode = Marshal.ReadInt32(lParam);
m_sendKeybind = (Keys)m_sendKeyCode;
Console.WriteLine("Keybind: " + (Keys)m_sendKeyCode);
Application.Exit();
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,
LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[Flags]
private enum KeyStates
{
None = 0,
Down = 1,
Toggled = 2
}
[DllImport("user32.dll")]
static extern short GetAsyncKeyState(Keys vKey);
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int SW_HIDE = 0;
const int SW_SHOW = 5;
[STAThread]
static void Main(string[] args)
{
setUp();
while (true)
{
if (GetAsyncKeyState(m_sendKeybind) < 0)
{
if (!m_hasSent)
{
if (!isHidden)
Console.WriteLine("Sending...");
int sentenceLength = rnd.Next(m_minWords, m_maxWords);
int wordLength = 0;
for (int i = 0; i < sentenceLength; i++)
{
if (rnd.Next(5) == 0) // Special keywords
{
if (i == sentenceLength - 1) // last word
{
tempWord = m_keyWords[rnd.Next(m_keyWords.Length)] + ".";
}
else
{
tempWord = m_keyWords[rnd.Next(m_keyWords.Length)] + " ";
}
}
else
{ // Normal word generation
wordLength = rnd.Next(m_minWordLength, m_maxWordLength);
for (int b = 0; b < wordLength; b++)
{
if (b == 0 && rnd.Next(2) == 0)
{
tempWord += m_russianUC[rnd.Next(m_russianUC.Length)];
}
else
{
tempWord += m_russianLC[rnd.Next(m_russianLC.Length)];
}
}
if (i == sentenceLength - 1) // last word
{
sentence += tempWord + m_marks[rnd.Next(0, m_marks.Length - 1)];
}
else
{
if (rnd.Next(4) == 0 && sentenceLength > 2)
{
tempWord += m_marks[rnd.Next(1, m_marks.Length)];
}
sentence += tempWord + " ";
}
tempWord = "";
}
}
if (sentence != null)
{
Clipboard.SetText(sentence);
SendKeys.SendWait("^V");
sentence = "";
}
m_hasSent = true;
}
}
else
{
m_hasSent = false;
}
Thread.Sleep(m_refresh);
}
}
static void setUp()
{
Console.WriteLine("Russian text generator");
bool doBind = false;
if (Properties.Settings.Default.sendKey != -1)
{
m_sendKeybind = (Keys)Properties.Settings.Default.sendKey;
Console.WriteLine("Found existing Keyconfig: " + m_sendKeybind +". Rebind? [y/n]:");
string input = Console.ReadLine().ToLower();
if (input != null && input.Length > 0)
{
doBind = input[0] == 'y';
}
}
else
doBind = true;
if (doBind)
{
_hookID = SetHook(_proc);
Console.WriteLine("Enter generate Key:");
Application.Run();
UnhookWindowsHookEx(_hookID);
Properties.Settings.Default.sendKey = m_sendKeyCode;
Properties.Settings.Default.Save();
}
Thread notifyThread = new Thread(
delegate ()
{
m_tray = new NotifyIcon()
{
Icon = Properties.Resources.Unbenannt_1,
Text = "Russian Text Generator"
};
m_tray.MouseClick += new MouseEventHandler(notifyIcon1_MouseClick);
m_tray.Visible = true;
Application.Run();
}
);
notifyThread.Start();
var handle = GetConsoleWindow();
// Hide
ShowWindow(handle, SW_HIDE);
isHidden = true;
}
public static void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
var handle = GetConsoleWindow();
if (isHidden)
{
ShowWindow(handle, SW_SHOW);
isHidden = !isHidden;
}
else
{
ShowWindow(handle, SW_HIDE);
isHidden = !isHidden;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment