DIY Second Keyboard (Script Triggering)

Here’s my idea:

  1. Plug a USB keyboard, possibly with custom labels on the keys, into a
  2. Raspberry Pi configured with XBindkeys to run
  3. a Python script that sends a trigger over a TCP/IP socket (via your LAN) to
  4. an AHK socket server that will then run the local AHK script desired.

Pros:

  • All parts can be re-purposed if the dedicated script-trigger keyboard is no longer needed.
  • Can be very inexpensive (I already own the hardware to do this).
  • Should be low-maintenance once working, as the Pi should auto-boot to the correct state and the info sent over TCP/IP should just indicate that a certain key was pressed.
  • I feel like I can handle the technology involved.

Cons:

  • I haven’t even started trying to see if this works.

Every so often when I’m using AutoHotKey scripts I wish I had a second keyboard dedicated to my various scripts. While something like the AHK Command Picker is probably all I need, the Enterpad is really more what I’ve been dreaming of:

enterpad_application_desktop_english_e2_thumb

Unless I’m given a free one like Daniel Schroeder, it’s pretty hard for me to justify close to $300. But a DIY setup? That might just happen.

DIY Second Keyboard (Script Triggering)

Example of using AutoHotKey instead of a batch file

On Super User I addressed a question about using a batch file to create a 7z file (like a ZIP file) of each individual file within a directory. That’s painful when you’re restricted to a Windows batch file, but once the questioner mentioned that AutoHotKey was an option, I thought:

PROBLEM SOLVED

AHK’s StrLen and SubStr can extract the variable portion of the file path. The file loop will recurse through all the files in the source directory. And then it’s just a matter of using RunWait to pass the paths to 7-Zip. The “,, Hide” specified at the end of the RunWait tells it to hide the command windows spawned.

InputBox, password, Enter Password for Archives, The generated archives will be protected with the password you enter below. Your input will be masked., hide
; Using FileSelectFolder is just one way of choosing your folders.
FileSelectFolder, sourcepath,,, Source Folder
sourcepath := RegExReplace(sourcepath, "\\$") ; Removes the trailing backslash, if present.
FileSelectFolder, destinationpath,,, Destination Folder
destinationpath := RegExReplace(destinationpath, "\\$") ; Removes the trailing backslash, if present.
sourcelen := StrLen(sourcepath) + 1 ; Determine the start of the variable part of the path.
Loop, Files, %sourcepath%\*.*, R
{
varfilepath := SubStr(A_LoopFileFullPath, sourcelen) ; Grab everything to the right of the source folder.
RunWait, "c:\program files\7-zip\7z.exe" a "%destinationpath%%varfilepath%.7z" "%A_LoopFileFullPath%" p"%password%" t7z mx0 mhe mmt,, Hide
FileCount := a_index
}
Msgbox Archives Created: %FileCount%`nSource: %sourcepath%`nDestination: %destinationpath%

I added in some pretty easy (in AHK) UI elements to enter a password, select the source and destination directories, and to be notified when the operation finished.

Note that you need v1.1.21+ of AHK or above for the file loop to operate as written.

Enter a password
Enter a password
Folder Selection
Folder Selection
Completion Summary
Completion Summary
Example of using AutoHotKey instead of a batch file

Paste through remote desktop connections

I have to tunnel through three remote desktop connections in order to run SSMS on a computer that is allowed to connect to a production SQL server. Somewhere in the tunnel I lose the ability to paste text from my local machine into the remote machine. Generally I’m just hunting for specific data, so it isn’t a big deal to compose new SQL for that purpose.

At other times, however, it’s a real pain having to retype a long query that I’ve already composed and run on a test instance. In those situations, I hit WIN + b, thanks to AutoHotKey:

#b:: ; Simulate typing of the text on the clipboard
KeyWait, LWin ; Ensure that the Windows key has been lifted.
KeyWait, RWin
ToPaste = %ClipBoard% ; Convert to text
Sleep 100
StringReplace, ToPaste, ToPaste, `r`n, `n, All ; Prevent CR+LF from causing two newlines
Sleep 100
SendRaw %ToPaste% ; SendRaw simulates typing the characters exactly, including AHK's special !^#+ characters
Return

You get to watch the computer do the typing, so be patient. If you press any keys or click around, this script will just keep on typing and mix in your live actions with what it’s typing.

You do have to watch out for the helpful features of whatever program you’re “pasting” into. Auto-indent exaggerates already-indented text that you “paste” and sometimes auto-complete suggestions are accepted in place of what you wanted.

Regardless, this little script saves time and frustration.

Paste through remote desktop connections

Clean Paste

While there are times that I appreciate the way copy-paste retains formatting in Windows, it very frequently causes headaches. Some of the applications in the Office suite provide the ability to paste without formatting, but there are still many times that unwanted formatting tags along or causes problems.

AutoHotKey to the rescue!

The following little script lets me hit the Windows key and “v” and get a clean paste without formatting:

; Copied from http://www.autohotkey.com/board/topic/10412-paste-plain-text-and-copycut/
#v:: ; Text–only paste from ClipBoard | WIN and V keys are the hotkey
KeyWait, LWin ; Wait for the Windows key to be lifted,
KeyWait, RWin ; since it would sometimes combine with the pasted text.
OldClip = %ClipBoardAll% ; Save formatted text for later
ClipBoard = %ClipBoard% ; Convert to text
Send ^v ; For best compatibility: SendPlay | simulates pressing CRTL and V keys
Sleep 50 ; Don't change clipboard while it is pasted! (Sleep > 0)
ClipBoard = %OldClip% ; Restore original ClipBoard
VarSetCapacity(OldClip, 0) ; Free memory
Return

view raw
AHK-PastePlain.ahk
hosted with ❤ by GitHub

This is pretty much just copied from a forum post. Nice and fast and no bad formatting.

Clean Paste