AutoHotKey script to input keyboard commands based on click at certain mouse co-ordinates.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
OrangeCat47
Posts: 1
Joined: 24 Apr 2018, 07:52

AutoHotKey script to input keyboard commands based on click at certain mouse co-ordinates.

24 Apr 2018, 08:08

I have written a simple Minesweeper program for a College assignment in C++.

I'll post a picture of the way my program works currently:

Image
mine-basic.PNG
mine-basic.PNG (24.81 KiB) Viewed 812 times
First the user selects what they'd like to do by pressing (f/p/r) then enter. Next they enter the co-ordinate they'd like to make that move in e.g. (E4) then once again press enter.

My main question is in the title.

I've used AutoHotKey a long time ago to write very basic scripts but recall it being a very powerful macro and remapping tool. I haven't used it for years as I mostly use Linux these day (at least at home for my desktop/web server/media server with raspberry pi), the ability to SSH to all of my local machines is invaluable to me, total control!! :) in College I use Windows as we're pretty much forced too - not a problem for me as I enjoy making my code cross platform compatible.

Sorry for the tangent.

My main question is would it be possible to make a basic script that will convert a mouse click at co-ordinate (143,565) to ("P" then ENTER), then ("E4" then ENTER).

I know this would be possible using ncurses/pdcurses or even windows specific API libraries and probably other methods but I don't want to over-complicate my code as the assignment calls for certain features and ONLY those features.

I feel an AHK script would be perfect for this but I'd just like to know if what I'm asking is possible with it?

I could probably learn AHK scripting much faster than incorporating libraries and adding to my C++ code.

The game also has an "expert" mode of play like this:

Image
mine-expert.PNG
mine-expert.PNG (14.91 KiB) Viewed 812 times
Another advantage of using an AHK to automate keyboard input would be that I could debug MUCH faster :)

Imagine playing an expert game and having to type in every co-ordinate! No thanks :) I've only ever completed a beginner game and that was painful enough.

Another point - not really relevant but the assignment only asks us to reveal one cell at a time, it doesn't use iteration/recursive functions to check and reveal all other cells (like how the classic minesweeper game works)., I would have liked to include this feature but what I've written is enough to achieve a distinction (well, I hope) and I have other assignments to complete (I've mostly been working on this and have finished it well before the rest of my class as I love programming.

Sorry for such a long post.

TL;DR - Is the question I've written in the title possible?

Some other questions: (sorry :) )

A) Also can I set the script to use co-ordinates from the current application rather than full screen co-ordinates?

B) Are there any applications I could use in Linux to perform similar functions? Or any other methods?

C) I'd like to post my source code somewhere for review by a more experienced coder. What's the best place to do this? This is the first full program I've ever written apart for basic messing around with C++ on and off throughout the years (Project Euler). Some of my methods are a bit whacky and nonstandard, the program needs to read and write to a file to keep scores, I'm sure there are more standard ways to do this, my method seems a little unorthodox but I don't know just how bad it is until my code's reviewed...
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: AutoHotKey script to input keyboard commands based on click at certain mouse co-ordinates.

24 Apr 2018, 10:10

Yes, it's totally possible, especially since the font is monospaced so the coordinates should be predictable. With MouseGetPos the coordinates are even relative to the active window by default. Basically what you'd do is use some math to turn the mouse coordinates into a cell location. For example (making up numbers here) assume the grid starts at X = 10 and each cell is 15 pixels wide, you could do something like this:

Code: Select all

MouseGetPos, aX, aY ; Let's assume the user clicked on cell F1 - the fifth horizontal cell - and their click had an X value of 81
aX -= 10  ; Subtract the left hand border of the grid; aX is now 71
aX := aX / 15 ; Divide by the width of each cell; aX is now 4.7
aX := Ceil(aX) ; Round up; aX is now 5, the correct grid number
You can then do the same sort of thing for aY to find the vertical position, and then translate that into keypresses. Of course, those three lines of math can be combined into one like this: aX := Ceil((aX-10)/15), but I split it up for readability.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: AutoHotKey script to input keyboard commands based on click at certain mouse co-ordinates.

24 Apr 2018, 11:40

Code: Select all

#NoEnv
#SingleInstance, Force
CoordMode, Mouse, Screen ;your call if you want absolute coords
SendMode, Input

LButton::
{
	MouseGetPos, x, y
	if (x = 143 && y = 565) {
		Send, p{Enter}e4
	}
return
}

Esc::ExitApp, 0

though if i were u id rather map the letters to the x-axis, the numbers to the y-axis and let the script figure out what combination of letters and numbers to send based on your mouse position

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: LepG, mapcarter, mikeyww, OrangeCat and 261 guests