From eadaa326edb197a4a0ae3b3ab8afe1604c371cfd Mon Sep 17 00:00:00 2001 From: felixhudson Date: Wed, 26 Apr 2023 14:22:19 +0930 Subject: [PATCH] Improved drawing of grid. Simulate left click --- README.md | 7 +++++ keypress.cs | 82 ++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 75 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 3b145e8..87a24fd 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,13 @@ This allows you to teleport your mouse to a new location by typing two letters. ## Installation Download the exe from the releases page. +## Usage +When launched a grid of letters will be shown. +Type these letters, and the mouse will be teleported there. +Adjust the postion with up down left right or hjkl +Enter performs a left click +Escape at anytime resets mouse to original location. + ## How I use it Place the keypress.exe file in your path Use autohotkey to launch it https://www.autohotkey.com/ diff --git a/keypress.cs b/keypress.cs index 0d84c68..eb21365 100644 --- a/keypress.cs +++ b/keypress.cs @@ -1,6 +1,10 @@ using System; using System.Windows.Forms; using System.Drawing; +using System.Runtime.InteropServices; + + + public partial class Form2 : Form { @@ -10,6 +14,12 @@ public partial class Form2 : Form private int space; private int first; private int second; + private Point origpos; + + private int xnudge; + private int ynudge; + private int nudge; + public Form2() { // change this to alter spacing @@ -22,6 +32,7 @@ public Form2() } this.first = -100; + this.second = -100; // capture keypress's in this window this.KeyPreview = true; @@ -40,8 +51,15 @@ public Form2() // remove window decorations this.ControlBox = false; this.Text = String.Empty; + origpos = Cursor.Position; + + xnudge = 0; + ynudge = 0; + nudge = 15; } + [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] + public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo); private void GridPaint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; @@ -54,33 +72,69 @@ private void GridPaint(object sender, PaintEventArgs e) { newx = x * this.space; for (int y = 0; y < h/this.space; y += 1) { newy = y * this.space; - g.FillRectangle(green, newx, newy, 50, 30); - g.DrawString("" + alphabet[x] + alphabet[y], tt, black, newx, newy); + // show all grid if first is not active + if (first < 0) { + g.FillRectangle(green, newx, newy, 50, 30); + g.DrawString("" + alphabet[x] + alphabet[y], tt, black, newx, newy); + + } else if (x == first & second < 0 ){ + g.FillRectangle(green, newx, newy, 50, 30); + g.DrawString("" + alphabet[x] + alphabet[y], tt, black, newx, newy); + } else if ((x == first) ^ (y == second)){ + g.FillRectangle(green, newx, newy, 50, 30); + g.DrawString("" + alphabet[x] + alphabet[y], tt, black, newx, newy); + + } } } } private void FormKeypress(object sender, KeyEventArgs e) { + int LeftDown = (int)0x00000002; + int LeftUp = (int)0x00000004; + + // Check if the Escape key was pressed + if (e.KeyCode == Keys.Escape) + { + // reset mouse to start location + Cursor.Position = origpos; + // Exit the application + Application.Exit(); + } // Console.Write("keypress " + e.KeyCode + "\r\n"); if ( first < 0 ) { - first = Array.IndexOf(alphabet, (char)e.KeyCode) * space; + first = Array.IndexOf(alphabet, (char)e.KeyCode); + this.Refresh(); - } else { + } else if (first > 0 & second < 0) { // we have our second letter - second = Array.IndexOf(alphabet, (char)e.KeyCode) * space; + second = Array.IndexOf(alphabet, (char)e.KeyCode); // Console.WriteLine(new Point(first,second)); - Cursor.Position = new Point(first+ 15,second + 10); - Application.Exit(); + Cursor.Position = new Point(first * space + 15,second * space + 10); + this.Refresh(); + // Application.Exit(); + } else if (first > 0 & second > 0){ + + if (e.KeyCode == Keys.Enter) { + this.WindowState = FormWindowState.Minimized; + mouse_event((uint)LeftDown,(uint)(first*space + 15),(uint)(second * space + 15),0,0); + mouse_event((uint)LeftUp,(uint)(first*space + 15),(uint)(second * space + 15),0,0); + Application.Exit(); + } else if ((char)e.KeyCode == 'H' | e.KeyCode == Keys.Left){ + xnudge -= nudge; + } else if ((char)e.KeyCode == 'L'| e.KeyCode == Keys.Right){ + xnudge += nudge; + } else if ((char)e.KeyCode == 'J'| e.KeyCode == Keys.Down){ + ynudge += nudge; + } else if ((char)e.KeyCode == 'K'| e.KeyCode == Keys.Up){ + ynudge -= nudge; + } + Cursor.Position = new Point(first * space + 15 + xnudge,second * space + 10 + ynudge); + } - int pos = Array.IndexOf(alphabet, (char)e.KeyCode); + // int pos = Array.IndexOf(alphabet, (char)e.KeyCode); //Console.WriteLine("pos" + pos); - // Check if the Escape key was pressed - if (e.KeyCode == Keys.Escape) - { - // Exit the application - Application.Exit(); - } } public static void Main() {