Skip to content

Commit

Permalink
Improved drawing of grid. Simulate left click
Browse files Browse the repository at this point in the history
  • Loading branch information
felixhudson committed Apr 26, 2023
1 parent 42c2527 commit eadaa32
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 14 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
82 changes: 68 additions & 14 deletions keypress.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;




public partial class Form2 : Form
{
Expand All @@ -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
Expand All @@ -22,6 +32,7 @@ public Form2()
}

this.first = -100;
this.second = -100;

// capture keypress's in this window
this.KeyPreview = true;
Expand All @@ -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;
Expand All @@ -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() {
Expand Down

0 comments on commit eadaa32

Please sign in to comment.