Dota 2 Wiki
Advertisement

Documentation for Module:Key press Jump to code ↴ [ edit | purge ]

Reality Rift icon.png
▶️ Planeshift.
The documentation for this module can be found at Template:Key press.
You may be forwarded to another wiki language, in case a translation is not available.


-- This module implements {{key press}}.

local kbdPrefix =
	'<span class="key"><kbd class=' ..
	'"keys" ' ..
	--'style="border: 1px solid #888; ' ..
	'border-radius: 0.4em; ' ..
	'box-shadow: 0 0.2em rgba(0, 0, 0, 0.3); ' ..
	--'background-color: #d6d6d6; ' ..
	--'background-image: linear-gradient(to bottom, #2a2a2a, #252525, #282828); ' ..
	'color: #eee; ' ..
	'padding: 0.1em 0.3em; ' ..
	'font-family: inherit; ' ..
	'font-size: 0.93em;">'

local kbdSuffix = '</kbd></span>'

local keyText = {
	['shiftqueue'] = '⇧ [[Controls#Shift-queue|<span style="color:#eee;">Shift-queue</span>]]',
	['double'] = '↓↓ [[Game settings#doubletap|<span style="color:#eee;">Double-tapping</span>]]',
	['double-tapping'] 	= '↓↓ [[Game settings#Game|<span style="color:#eee;">Double-tapping</span>]]',
	['autoattack/never'] = '◎ [[Auto attack|<span style="color:#eee;">Never</span>]]',
	['autoattack/standard'] = '◉ [[Auto attack|<span style="color:#eee;">Standard</span>]]',
	['autoattack/always'] = '◎ [[Auto attack|<span style="color:#eee;">Always</span>]]',
	['hotkey'] = '[[Hotkey|<span style="color:#eee;">Hotkey</span>]]',
	['hotkeys'] = '[[Hotkeys|<span style="color:#eee;">Hotkeys</span>]]',
	['buyback'] = '[[Mechanics#Buyback|<span style="color:#eee; font-weight:bold">BUYBACK</span>]]',
	['disablehelp'] = '☐ [[Head-up display#Shared Unit Control|<span style="color:#eee;">Disabled Help</span>]]',
	['enablehelp'] = '▣ [[Head-up display#Shared Unit Control|<span style="color:#eee;">Enabled Help</span>]]',
	['ping'] = '‼ [[Controls#Communication|<span style="color:#eee;">Ping</span>]]',
	['pingx'] = '✖ [[Minimap#Pinging|<span style="color:#eee;">Ping (Danger)</span>]]',
	['mouseleft'] = '[[File:Keyboard White Mouse Left.png|18px]] Left Click',
	['mouseright'] = '[[File:Keyboard White Mouse Right.png|18px]] Right Click',
	['mousemid'] = '[[File:Keyboard White Mouse Middle.png|18px]] Middle Click',
	['mousescroll'] = '[[File:Keyboard White Mouse Middle.png|18px]] ↕ Scroll Wheel',
	['mousescrollup'] = '[[File:Keyboard White Mouse Middle.png|18px]] ↑ Scroll Up',
	['mousescrolldown'] = '[[File:Keyboard White Mouse Middle.png|18px]] ↓ Scroll Down',
	['mouse'] = '[[File:Keyboard White Mouse None.png|18px]] Mouse',
	['portraitcast'] = '[[File:Self-cast cursor (black).png|18px]] Portrait-casting',
	['selfcast'] = '[[File:Self-cast cursor (black).png|18px]] Self-casting',
	['legacy'] = '[[Hotkeys#Basic Hotkeys|<span style="color:#999900; font-weight:bolder">L</span><span style="color:#eee;">egacy Keys</span>]]',
	['drag'] = '⇱⇲ Drag',
	['scroll'] = '↕ Scroll',
	['hold'] = '⤓ Hold',
	['blank'] = '<blank>',
	['caps lock'] = '⇪ Caps Lock',
	['shift'] = '⇧ Shift',
	['enter'] = '↵ Enter',
	['cmd'] = '⌘ Cmd',
	['command'] = '⌘ Command',
	['opt'] = '⌥ Opt',
	['option'] = '⌥ Option',
	['tab'] = 'Tab ↹',
	['backspace'] = '← Backspace',
	['win'] = '⊞ Win',
	['menu'] = '≣ Menu',
	['up'] = '↑',
	['down'] = '↓',
	['left'] = '←',
	['right'] = '→',
	['asterisk'] = '&#42;',
	['hash'] = '&#35;',
	['colon'] = '&#58;',
	['pipe'] = '&#124;',
	['semicolon'] = '&#59;',
	['equals'] = '&#61;',
}

local keyAlias = {
	-- ['alternate name for key (alias)'] = 'name for key used in key table'
	['*'] = 'asterisk',
	['#'] = 'hash',
	[':'] = 'colon',
	[';'] = 'semicolon',
}

local Collection = {}
Collection.__index = Collection
do
	function Collection:add(item)
		if item ~= nil then
			self.n = self.n + 1
			self[self.n] = item
		end
	end
	function Collection:join(sep)
		return table.concat(self, sep)
	end
	function Collection:sort(comp)
		table.sort(self, comp)
	end
	function Collection.new()
		return setmetatable({n = 0}, Collection)
	end
end

local function keyPress(args)
	local chainNames = {
		'chain first',
		'chain second',
		'chain third',
		'chain fourth',
		'chain fifth',
		'chain sixth',
		'chain seventh',
		'chain eighth',
		'chain ninth',
	}
	local result = Collection.new()
	local chainDefault = args.chain or '+'
	for i, id in ipairs(args) do
		if i > 1 then
			result:add(args[chainNames[i - 1]] or chainDefault)
		end
		local lc = id:lower()
		local text = keyText[lc] or keyText[keyAlias[lc]] or id
		result:add(kbdPrefix .. text .. kbdSuffix)
	end
	return result:join()
end

local function keypress(frame)
	-- Called by "{{key press|...}}".
	-- Using the template doubles the post‐expand include size.
	return keyPress(frame:getParent().args)
end

local function press(frame)
	-- Called by "{{#invoke:key|press|...}}".
	return keyPress(frame.args)
end

return {
	keypress = keypress,
	press = press,
}
Advertisement