Sending keystrokes and mouse clicks using Scala - scala

I've previously used AutoHotkey to send keystrokes and mouse clicks to automate interaction with GUIs and browsers. How do I send keystrokes and mouse clicks this time using Scala?

You can use java.awt.Robot.
val robot = new java.awt.Robot
robot.mouseMove(100,100) // Cursor will jump to the top left of your screen
Note that you have to enter text keycode by keycode (see java.awt.event.KeyEvent). This is pretty annoying, but you can write code to automate the pressing and releasing of keys. (You do need to release every key you press! It's emulating the keyboard....)

JNA + Windows API
I had to send input to a background window and it worked really well for me.

Related

Basic AutoHotKey Use For Multiple Key Presses

How can I use AutoHotKey to simulate pressing control quote, then the menu bar button, then the down arrow when I press control p?
As a beginner, I have just been fiddling around with it, but I did not manage to make it work.
>^p::
Send ^{'}
Send {AppsKey}
Send {Down}
Right now, to me at least, it seems as though the only line of the code that is executing is "Send ^{'}" without anything under it executing.

What's the difference between Send and ControlSend?

AutoHotkey's official documentation lists two different sets of commands for sending simulated keyboard input to a window.
Send / SendRaw / SendInput / SendPlay / SendEvent
Sends simulated keystrokes and mouse clicks to the active window.
ControlSend / ControlSendRaw
Sends simulated keystrokes to a window or control.
What's the difference between Send and ControlSend?
Is there a reason to use one over the other?
Send/SendXXX commands send input to the active window. That is the window that currently has focus, usually by clicking it with your mouse, tabbing to it, or when a window sets focus to itself.
If your AHK script were to target a Notepad window that you have open, and you were to click on another window such as Chrome, your inputs would now be sent to Chrome.
On the flipside, using ControlSend/ControlSendXXX commands will send input to a specified window or control. A control might be a textbox, button, or similar interactive elements.
Here, the above example would still output to Notepad even if you switched focus to another window such as Chrome. The downside is that you must specify which control to send to.

In notepad++ change tab size instantly

I'm using notepad++ (v6.5.3) and I constantly have to change the size of the tab for viewing some results. Not that it's taking me a lot of time to do it manually everytime, but it would be great if I could optimize that.
Is there a way to do so? Would a macro be the solution, or are they just for typing stuff?
Thanks a lot!
Ok, there's an easy way how you can achieve this - I have tested it right now:
Install AutoHotKey (or start portable version which runs without installation)
In Windows 7 and above, ensure you launched AutoHotKey as Administrator (otherwise you get inconsistencies in its behavior) - if not sure, exit it and restart it as administrator
Right click Autohotkey tray icon and select Edit This Script
Import the macro below this list at the end of the AutoHotKey script file and save the file
Right click Autohotkey tray icon and select Reload This Script.
–– This was end of general steps, now let's go with your macro: ––
In N++, display Preferences window and press its Close button1 at the bottom (NOT at the top-right corner)
Now you can use shortcuts Win+F2 and Win+F3 to switch different tab sizes instantly
SendMode Input
DetectHiddenWindows, On
SetTitleMatchMode, RegEx
;--------------------------------- Hotkeys for Notepad++ only
#IfWinActive ahk_class Notepad\+\+
#F2::Send {F10}{Right 6}{Down}{Enter}{Tab 2}{Space}16{Enter}{Tab 3}{Space}
#F3::Send {F10}{Right 6}{Down}{Enter}{Tab 2}{Space}4{Enter}{Tab 3}{Space}
#IfWinActive
1) Important: N++ user experience provided in dialog boxes is absolutely terrible. There are no anchors where you can fix focus when using keyboard. Thus you always need to perform step 4 manually when leaving Preferences dialog box otherwise the macros would send keys into incorect window page OR at correct page but incorrect control. Preferences dialog window remembers selected page and control. Macros I created for you therefore assume that correct page is already listed and button Close was recently focused.
Good news is Notepad++ windows with this weird behavior are rare exception from general user experience. In other places in N++ (or in other apps) where user interface components (menus, dialogs etc.) always start from the same point you do not need any special precautions like the one in step 4.
Adjust the macros as you like:
you can create more of them
you can adjust the numbers "16" and "4" typed into tab size input box
you can change shortcut keys to something else
you can replace sending keys with sending mouse clicks at desired screen/window positions
you can achieve many other useful shortcuts in N++ and in all other apps – check AHK deeper!

Menu Item in AHK

I'm switching from keyboard maestro and mac, to AHK and Windows.
I'd like to choose a menu item in Thunderbird, say,
"Message>Move to>me#email.com>Archive".
Or I'd like to choose bookmark "2checkvist" in Chrome.
How do I create hotkeys to menu items? And how do I keep them to only that program, so that alt+t in chrome can do one thing, while alt+t in thunderbird does another thing?
As Jan Dvorak says, look at ifWinActive in the docs (I should get a point for posting the link to docs, you know).
Then you can use send with the alt key, something like this (you have to expiriment).
ifwinexist thunderbird
{
winactivate thunderbird
send !m ;message menu
send m ;move to (if you have more m-commands, then you have to send this more than once till you get to it)
send something ;here put keys until you get to your account
send a ;archive
}
Basically, you want ahk to use the alt menu keys. Everyone's interface differs a little depending on your addons and version. In your main interface, hold down the alt key and look at your menu bar. You will see that little underlines appear under certain letters. This is your "alt" key command. Now, figure out the sequence. You only need to push alt once to open the "alt thread" - now navigate manually through the menus using your keyboard. Now translate that sequence into autohotkey send commands.
You might also have to use activateWindow to assure your alt key will take correctly.

Autohotkey - capture extra mouse buttons

Can autohotkey capture nonstandard mouse buttons? I use a five-button mouse (Microsoft Wireless Laser Mouse 6000).
XButton1 and XButton2 according to the documentation on autohotkey.com.
The following URLs show how to have autohotkey log all keyboard and mouse events, and how to look at the log autohotkey generates of those events.
http://www.autohotkey.com/docs/commands/_InstallMouseHook.htm
http://www.autohotkey.com/docs/KeyList.htm#SpecialKeys (special keys section)
Based on this, you can find out about all mouse and keyboard events by creating an autohotkey script as such:
#InstallKeybdHook
#InstallMouseHook
Once you run the script, you can double click on the tray icon for that script, then go to View > Key History and Script Info (Ctrl K)
Based on this information, I figured out that my mouse driver is already redefining the extra mouse buttons to other keys. However, I can re-map those keys by going to Control Panel > Mouse, selecting the desired button, and using the "Macro..." option in the mouse configuration (this is a special configuration only for the Microsoft Wireless Laser Mouse 6000 v2). In the macro dialog, I can define keystrokes for those mouse buttons to send (only one per mouse button). Next, I can use AutoHotkey to watch for whatever keystrokes I have defined, and perform specific actions based on those keystrokes.
You need to capture the scancode of the key and then use that. You can find a script in 5th post of this thread, written by Skan, which will allow you to do this. Just run that and click on the GUI with the mouse button you wish to determine the scancode. Then use the scancode in place of the normal key when you create the hotkey.
There is also a built in method of retrieving keys which is documented at the bottom of this page under the heading "Special Keys". Essentially, AHK logs your key presses and automatically records the scancodes for you.
To use the scancode as a hotkey, you just do the following:
SC###:: ;Your code here
Where ### is replaced with the code of your key (or mouse button).