To Share: My script for MS Office accent mark shortcuts

Post your working scripts, libraries and tools for AHK v1.1 and older
VictoryRice
Posts: 2
Joined: 08 Jun 2017, 19:22

To Share: My script for MS Office accent mark shortcuts

08 Jun 2017, 23:03

EDIT Feb 2020 -- replaced with new version that fixes an issue when CAPS LOCK is on
---
I particularly like Microsoft's system for inputting commonly accented/international characters (and other useful symbols) in Word/Outlook/etc., but of course, they only work in Office products. AHK to the rescue then (though I have to imagine someone has written this before).

The shortcuts implemented are here: https support.office.com /en-us/article/keyboard-shortcuts-for-international-characters-108fa0c1-fb8e-4aae-9db1-d60407d13c35 Broken Link for safety
And I added a few from here: https support.office.com /en-us/article/Keyboard-shortcuts-for-Microsoft-Word-on-Windows-95EF89DD-7142-4B50-AFB2-F762F663CEB2 Broken Link for safety

Hope someone else finds it useful, or can improve/extend it.

Code: Select all

; -------- Autohotkey script: Use Microsoft Office keyboard shortcuts for international characters in any context
; -------- For list of these shortcuts, visit https support.office.com /en-us/article/keyboard-shortcuts-for-international-characters-108fa0c1-fb8e-4aae-9db1-d60407d13c35  Broken Link for safety
; -------- Also does the following character shortcuts: em dash, en dash, elipsis, copyright, trademark, registered trademark
; -------- These are described here: https support.office.com /en-us/article/Keyboard-shortcuts-for-Microsoft-Word-on-Windows-95EF89DD-7142-4B50-AFB2-F762F663CEB2  Broken Link for safety
; --------
; -------- (c) 2020 VictoryRice: no rights reserved :-) 
; -------- Thanks to user Laszlo https://autohotkey.com/board/topic/14014-help-with-script-to-emulate-apple-keyboard-syle-accent-marks/
;
; Note 1: For the accents/diacritics, you have 2 seconds after pressing the dead key to press the letter you want accented.
; Note 2: A limitation of this script is that after pressing the dead key, pressing an accent-able letter within 2 seconds
;         will accent the character *even if you've pressed another key in the meantime*. (Unless that other key is also
;         accent-able by a different dead key monitored by this script.) I couldn't think of an efficient way to solve this,
;         but given that this would rarely, if ever, happen, I didn't try very hard either ;-).


#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#UseHook

; ---- Workaround for Ctrl-: dead key (control-colon)
; ---- Must be at top of script
; ---- alternatively could have mapped to ^+; on standard US keyboard layouts
; ---- other layouts may not require Shift for the colon key though
Hotkey, ^:, DoNothing
Return
DoNothing:
Return

; ---- Set up the other dead keys to do nothing
^`::Return
^'::Return
^^::Return
^~::Return
;^+;::Return
^@::Return
^&::Return
^,::Return
^/::Return

; ---- Set up the accent-able keys that will work with at least one of the dead key combos above
a::f()
c::f()
d::f()
e::f()
i::f()
n::f()
o::f()
s::f()
u::f()
y::f()
+a::f()
+c::f()
+d::f()
+e::f()
+i::f()
+n::f()
+o::f()
+u::f()
+y::f()

; ---- Set up the shortcuts that don't require a dead key to be pressed first
; ------- Inverted punctuation
!^?::
  SendInput ¿
  Return
!^!::
  SendInput ¡
  Return
; ------- Dashes etc.
^NumpadSub::
  SendInput –   ;  en-dash
  Return
^!NumpadSub::
  SendInput —   ;  em-dash
  Return
^!.::
  SendInput …   ; elipsis
  Return
; ------- Symbols
^!c::
  SendInput ©   ; copyright
  Return
^!r::
  SendInput ®   ; registered trademark
  Return
^!t::
  SendInput ™   ; trademark
  Return
^!e::
  SendInput €   ; Euro
  Return


; --------------- The logic combining dead keys with accent-able keys
f() {
	kts = % A_ThisHotkey     ;  kts = Key To Send

	; ------- Accent Grave -------
	if (A_PriorHotkey = "^``" && A_TimeSincePriorHotkey < 2000) {
		if (A_ThisHotkey = "a") 
			kts = à
		else if (A_ThisHotkey = "e")
			kts = è
		else if (A_ThisHotkey = "e")
			kts = è
		else if (A_ThisHotkey = "i")
			kts = ì
		else if (A_ThisHotkey = "o")
			kts = ò
		else if (A_ThisHotkey = "u")
			kts = ù
		else if (A_ThisHotkey = "+a")
			kts = À
		else if (A_ThisHotkey = "+e")
			kts = È
		else if (A_ThisHotkey = "+i")
			kts = Ì
		else if (A_ThisHotkey = "+o")
			kts = Ò
		else if (A_ThisHotkey = "+u")
			kts = Ù
	}

	; ------- Accent Acute -------
	else if (A_PriorHotkey = "^'" && A_TimeSincePriorHotkey < 2000) {
		if (A_ThisHotkey = "a") 
			kts = á
		else if (A_ThisHotkey = "e")
			kts = é
		else if (A_ThisHotkey = "i")
			kts = í
		else if (A_ThisHotkey = "o")
			kts = ó
		else if (A_ThisHotkey = "u")
			kts = ú
		else if (A_ThisHotkey = "y")
			kts = ý
		else if (A_ThisHotkey = "+a")
			kts = Á
		else if (A_ThisHotkey = "+e")
			kts = É
		else if (A_ThisHotkey = "+i")
			kts = Í
		else if (A_ThisHotkey = "+o")
			kts = Ó
		else if (A_ThisHotkey = "+u")
			kts = Ú
		else if (A_ThisHotkey = "+y")
			kts = Ý
		; ------- Eth -------
		else if (A_ThisHotkey = "d")
			kts = ð
		else if (A_ThisHotkey = "+d")
			kts = Ð
	}	

	; ------- Circumflex -------
	if (A_PriorHotkey = "^^" && A_TimeSincePriorHotkey < 2000) {
		if (A_ThisHotkey = "a") 
			kts = â
		else if (A_ThisHotkey = "e")
			kts = ê
		else if (A_ThisHotkey = "e")
			kts = è
		else if (A_ThisHotkey = "i")
			kts = î
		else if (A_ThisHotkey = "o")
			kts = ô
		else if (A_ThisHotkey = "u")
			kts = û
		else if (A_ThisHotkey = "+a")
			kts = Â
		else if (A_ThisHotkey = "+e")
			kts = Ê
		else if (A_ThisHotkey = "+i")
			kts = Î
		else if (A_ThisHotkey = "+o")
			kts = Ô
		else if (A_ThisHotkey = "+u")
			kts = Û
	}

	; ------- Tilde -------
	if (A_PriorHotkey = "^~" && A_TimeSincePriorHotkey < 2000) {
		if (A_ThisHotkey = "a") 
			kts = ã
		else if (A_ThisHotkey = "n")
			kts = ñ
		else if (A_ThisHotkey = "o")
			kts = õ
		else if (A_ThisHotkey = "+a")
			kts = Ã
		else if (A_ThisHotkey = "+n")
			kts = Ñ
		else if (A_ThisHotkey = "+o")
			kts = Õ
	}

	; ------- Umlaut/Diaeresis -------
	else if (A_PriorHotkey = "^:" && A_TimeSincePriorHotkey < 2000) {
		if (A_ThisHotkey = "a") 
			kts = ä
		else if (A_ThisHotkey = "e")
			kts = ë
		else if (A_ThisHotkey = "i")
			kts = ï
		else if (A_ThisHotkey = "o")
			kts = ö
		else if (A_ThisHotkey = "u")
			kts = ü
		else if (A_ThisHotkey = "y")
			kts = ÿ
		else if (A_ThisHotkey = "+a")
			kts = Ä
		else if (A_ThisHotkey = "+e")
			kts = Ë
		else if (A_ThisHotkey = "+i")
			kts = Ï
		else if (A_ThisHotkey = "+o")
			kts = Ö
		else if (A_ThisHotkey = "+u")
			kts = Ü
		else if (A_ThisHotkey = "+y")
			kts = Ÿ
	}

	; ------- Ring -------
	else if (A_PriorHotkey = "^@" && A_TimeSincePriorHotkey < 2000) {
		if (A_ThisHotkey = "a") 
			kts = å
		else if (A_ThisHotkey = "+a")
			kts = Å
	}

	; ------- Ligature -------
	else if (A_PriorHotkey = "^&" && A_TimeSincePriorHotkey < 2000) {
		if (A_ThisHotkey = "a") 
			kts = æ
		else if (A_ThisHotkey = "+a")
			kts = Æ
		else if (A_ThisHotkey = "o") 
			kts = œ
		else if (A_ThisHotkey = "+O")
			kts = Œ
		else if (A_ThisHotkey = "s")
			kts = ß
	}

	; ------- Cedilla -------
	else if (A_PriorHotkey = "^," && A_TimeSincePriorHotkey < 2000) {
		if (A_ThisHotkey = "c") 
			kts = ç
		else if (A_ThisHotkey = "+c")
			kts = Ç
	}

	; ------- Slashed O -------
	else if (A_PriorHotkey = "^/" && A_TimeSincePriorHotkey < 2000) {
		if (A_ThisHotkey = "o") 
			kts = ø
		else if (A_ThisHotkey = "+o")
			kts = Ø
	}
	
	; ------- Correctly handle CAPS LOCK with and without Shift held -------
	if GetKeyState("Capslock", "T") {
		if pos := InStr(kts, "+") {
			kts := SubStr(kts,pos+1)
		}
		else {
			kts := "+" . kts
		}
	}

	SendInput % kts
}

Last edited by VictoryRice on 28 Feb 2020, 03:22, edited 1 time in total.
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: To Share: My script for MS Office accent mark shortcuts

09 Jun 2017, 07:47

@VictoryRice,
Great script. I like it a lot.

As it's written, I got random failures on single quotes after the "^'" combination has been used, usuall following a control-colon sequence but not always.

I added the following code in your autoexecute section, and it seems to have cleared it up. US 105-key standard layout here, Win7 64-bit, AHK_H.

Code: Select all

#UseHook

; ---- Workaround for Ctrl-: dead key (control-colon)
; ---- Must be at top of script
; ---- alternatively could have mapped to ^+; on standard US keyboard layouts
; ---- other layouts may not require Shift for the colon key though
Hotkey, ^:, DoNothing
Return
Hotkey, ^', DoNothing <<<<<<<<<<--------------------------------------------------------<
; --- Added the line above to keep the single quote intact after use.
; Hotkey, ^`', DoNothing ; also worked.
DoNothing:
Return
Am I off-base there?

Thanks,
burque505
VictoryRice
Posts: 2
Joined: 08 Jun 2017, 19:22

Re: To Share: My script for MS Office accent mark shortcuts

14 Jun 2017, 10:59

Hey thanks -- I don't know enough about AHK to speculate as to why it might be failing for you, or why your fix works. I'm using version 1.1.25.02, Windows 10 64-bit, US keyboard layout.
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: To Share: My script for MS Office accent mark shortcuts

14 Jun 2017, 14:26

I'm not really sure why it fixed it for me either, but I really like your script a lot, thanks again.
tvanyo
Posts: 1
Joined: 05 Apr 2018, 22:02

Re: To Share: My script for MS Office accent mark shortcuts

04 May 2018, 11:17

Thanks for the script. Have found several in the forums, but this one seems to be working the most consistently.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: sanmaodo and 170 guests