Module:Keypress

-- -- This template is based heavily on Wikipedia's Template:Key_press and Template:Key_press/core -- Invoked by Template:Keypress --

local getArgs = require('Module:Arguments').getArgs local yesno = require('Module:Yesno') local p = {}

function p.key_press( frame ) local args = getArgs(frame) local chain = args.chain or '+' local nocap = yesno(args.nocap, false) local key_presses = mw.html.create(''):done key_presses:node(p._core(args[1] or '', nocap)) for i = 2, 10, 1 do		local key = args[i] if key then key_presses:wikitext(chain) key_presses:node(p._core(key, nocap)) end end return tostring(key_presses) end

-- Returns mw.html kbd function p._core( pKey, nocap ) local key = string.lower(pKey) local kbd = mw.html.create('kbd') :addClass('keypress-diagram') :done local function unicode( symbol ) return mw.html.create('span'):addClass('unicode'):wikitext(symbol):done end -- http://stackoverflow.com/a/20285006/1411473 local function firstLettersToUpper(str, nocap) return nocap and str or string.gsub(' '..str, '%W%l', string.upper):sub(2) end if    key == 'caps lock'				then kbd:node(unicode('⇪')):wikitext(' Caps Lock') elseif key == 'shift'					then kbd:node(unicode('⇧')):wikitext(' Shift') elseif key == 'enter'					then kbd:node(unicode('↵')):wikitext(' Enter') elseif key == 'cmd' or key == 'command'	then kbd:node(unicode('⌘')):wikitext(' '..firstLettersToUpper(key, nocap)) elseif key == 'opt' or key == 'option'	then kbd:node(unicode('⌥')):wikitext(' '..firstLettersToUpper(key, nocap)) elseif key == 'tab'						then kbd:wikitext('Tab '):node(unicode('↹')) elseif key == 'backspace'				then kbd:wikitext('← Backspace') elseif key == 'win'						then kbd:node(unicode('⊞')):wikitext(' Win') elseif key == 'menu'					then kbd:node(unicode('≣')):wikitext(' Menu') elseif key == 'up'						then kbd:wikitext('↑') elseif key == 'down'					then kbd:wikitext('↓') elseif key == 'left'					then kbd:wikitext('←') elseif key == 'right'					then kbd:wikitext('→') elseif key == '*' or key == 'asterisk'	then kbd:wikitext('*') elseif key == '#' or key == 'hash'		then kbd:wikitext('#') elseif key == ':' or key == 'colon'		then kbd:wikitext(':') elseif key == 'pipe'					then kbd:wikitext('|') elseif key == ';' or key == 'semicolon'	then kbd:wikitext(';') elseif key == 'equals'					then kbd:wikitext('=') else									    kbd:wikitext(firstLettersToUpper(key, nocap)) end return kbd; end

return p