置顶公告:【置顶】关于临时开启评论区所有功能的公告(2022.10.22) | 【置顶】关于本站Widget恢复使用的公告
  • 你好~!欢迎来到萌娘百科镜像站!如需查看或编辑,请联系本站管理员注册账号。
  • 本镜像站和其他萌娘百科的镜像站无关,请注意分别。

Module:Key

猛汉♂百科,万男皆可猛的百科全书!转载请标注来源页面的网页链接,并声明引自猛汉百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [创建] [刷新]
  1. -- This module implements {{key press}}.
  2. local kbdPrefix =
  3. '<kbd class=' ..
  4. '"keyboard-key nowrap" ' ..
  5. 'style="border: 1px solid #aaa; ' ..
  6. -- The following is an expansion of {{border-radius|0.2em}}
  7. '-moz-border-radius: 0.2em; -webkit-border-radius: 0.2em; border-radius: 0.2em; ' ..
  8. -- The following is an expansion of {{box-shadow|0.1em|0.1em|0.2em|rgba(0,0,0,0.1)}}
  9. '-moz-box-shadow: 0.1em 0.1em 0.2em rgba(0,0,0,0.1); -webkit-box-shadow: 0.1em 0.1em 0.2em rgba(0,0,0,0.1); box-shadow: 0.1em 0.1em 0.2em rgba(0,0,0,0.1); ' ..
  10. 'background-color: #f9f9f9; ' ..
  11. -- The following is an expansion of {{linear-gradient|top|#eee, #f9f9f9, #eee}}
  12. 'background-image: -moz-linear-gradient(top, #eee, #f9f9f9, #eee); background-image: -o-linear-gradient(top, #eee, #f9f9f9, #eee); background-image: -webkit-linear-gradient(top, #eee, #f9f9f9, #eee); background-image: linear-gradient(to bottom, #eee, #f9f9f9, #eee); ' ..
  13. -- Force black color to fix [[phab:T200258]]
  14. 'color: #000; ' ..
  15. 'padding: 0.1em 0.3em; ' ..
  16. 'font-family: inherit; ' ..
  17. 'font-size: 0.85em;">'
  18. local kbdSuffix = '</kbd>'
  19. local keyText = {
  20. ['caps lock'] = '⇪ Caps Lock',
  21. ['[[caps lock]]'] = '⇪ [[Caps Lock]]',
  22. ['shift'] = '⇧ Shift',
  23. ['[[shift key|shift]]'] = '⇧ [[Shift key|Shift]]',
  24. ['enter'] = '↵ Enter',
  25. ['[[enter key|enter]]'] = '↵ [[Enter key|Enter]]',
  26. ['cmd'] = '⌘ Cmd',
  27. ['[[command key|cmd]]'] = '⌘ [[Command key|Cmd]]',
  28. ['command'] = '⌘ Command',
  29. ['[[command key|command]]'] = '⌘ [[Command key|Command]]',
  30. ['opt'] = '⌥ Opt',
  31. ['[[option key|opt]]'] = '⌥ [[Option key|Opt]]',
  32. ['option'] = '⌥ Option',
  33. ['[[option key|option]]'] = '⌥ [[Option key|Option]]',
  34. ['tab'] = 'Tab ↹',
  35. ['[[tab key|tab]]'] = '[[Tab key|Tab]] ↹',
  36. ['backspace'] = '← Backspace',
  37. ['[[backspace]]'] = '← [[Backspace]]',
  38. ['win'] = '⊞ Win',
  39. ['[[windows key|win]]'] = '⊞ [[Windows key|Win]]',
  40. ['menu'] = '≣ Menu',
  41. ['[[menu key|menu]]'] = '≣ [[Menu key|Menu]]',
  42. ['up'] = '↑',
  43. ['[[arrow keys|up]]'] = '[[Arrow keys|↑]]',
  44. ['down'] = '↓',
  45. ['[[arrow keys|down]]'] = '[[Arrow keys|↓]]',
  46. ['left'] = '←',
  47. ['[[arrow keys|left]]'] = '[[Arrow keys|←]]',
  48. ['right'] = '→',
  49. ['[[arrow keys|right]]'] = '[[Arrow keys|→]]',
  50. ['asterisk'] = '&#42;',
  51. ['hash'] = '&#35;',
  52. ['[[#]]'] = '[[Number sign|#]]',
  53. ['colon'] = '&#58;',
  54. ['[[:]]'] = '[[Colon (punctuation)|:]]',
  55. ['pipe'] = '&#124;',
  56. ['[[|]]'] = '[[Pipe symbol|&#124;]]',
  57. ['semicolon'] = '&#59;',
  58. ['[[;]]'] = '[[Semi-colon|&#59;]]',
  59. ['equals'] = '&#61;',
  60. -- Left & right analog sticks.
  61. ['l up'] = 'L↑',
  62. ['l down'] = 'L↓',
  63. ['l left'] = 'L←',
  64. ['l right'] = 'L→',
  65. ['l ne'] = 'L↗',
  66. ['l se'] = 'L↘',
  67. ['l nw'] = 'L↖',
  68. ['l sw'] = 'L↙',
  69. ['r up'] = 'R↑',
  70. ['r down'] = 'R↓',
  71. ['r left'] = 'R←',
  72. ['r right'] = 'R→',
  73. ['r ne'] = 'R↗',
  74. ['r se'] = 'R↘',
  75. ['r nw'] = 'R↖',
  76. ['r sw'] = 'R↙',
  77. -- PlayStation.
  78. ['ex'] = '×',
  79. ['circle'] = '○',
  80. ['square'] = '□',
  81. ['triangle'] = '△',
  82. -- Nintendo 64 and GameCube.
  83. ['c up'] = 'C↑',
  84. ['c down'] = 'C↓',
  85. ['c left'] = 'C←',
  86. ['c right'] = 'C→',
  87. ['c ne'] = 'C↗',
  88. ['c se'] = 'C↘',
  89. ['c nw'] = 'C↖',
  90. ['c sw'] = 'C↙',
  91. }
  92. local keyAlias = {
  93. -- ['alternate name for key (alias)'] = 'name for key used in key table'
  94. ['[[cmd key|cmd]]'] = '[[command key|cmd]]',
  95. ['[[cmd key|command]]'] = '[[command key|command]]',
  96. ['[[opt key|opt]]'] = '[[option key|opt]]',
  97. ['[[option key]]'] = '[[option key|option]]',
  98. ['[[opt key|option]]'] = '[[option key|option]]',
  99. ['[[win key|win]]'] = '[[windows key|win]]',
  100. ['*'] = 'asterisk',
  101. ['#'] = 'hash',
  102. [':'] = 'colon',
  103. [';'] = 'semicolon',
  104. ['l-up'] = 'l up',
  105. ['l-down'] = 'l down',
  106. ['l-left'] = 'l left',
  107. ['l-right'] = 'l right',
  108. ['l-ne'] = 'l ne',
  109. ['l-se'] = 'l se',
  110. ['l-nw'] = 'l nw',
  111. ['l-sw'] = 'l sw',
  112. ['r-up'] = 'r up',
  113. ['r-down'] = 'r down',
  114. ['r-left'] = 'r left',
  115. ['r-right'] = 'r right',
  116. ['r-ne'] = 'r ne',
  117. ['r-se'] = 'r se',
  118. ['r-nw'] = 'r nw',
  119. ['r-sw'] = 'r sw',
  120. ['ps x'] = 'ex',
  121. ['ps c'] = 'circle',
  122. ['ps s'] = 'square',
  123. ['ps t'] = 'triangle',
  124. ['c-up'] = 'c up',
  125. ['c-down'] = 'c down',
  126. ['c-left'] = 'c left',
  127. ['c-right'] = 'c right',
  128. ['c-ne'] = 'c ne',
  129. ['c-se'] = 'c se',
  130. ['c-nw'] = 'c nw',
  131. ['c-sw'] = 'c sw',
  132. }
  133. local Collection = {}
  134. Collection.__index = Collection
  135. do
  136. function Collection:add(item)
  137. if item ~= nil then
  138. self.n = self.n + 1
  139. self[self.n] = item
  140. end
  141. end
  142. function Collection:join(sep)
  143. return table.concat(self, sep)
  144. end
  145. function Collection:sort(comp)
  146. table.sort(self, comp)
  147. end
  148. function Collection.new()
  149. return setmetatable({n = 0}, Collection)
  150. end
  151. end
  152. local function keyPress(args)
  153. local chainNames = {
  154. 'chain first',
  155. 'chain second',
  156. 'chain third',
  157. 'chain fourth',
  158. 'chain fifth',
  159. 'chain sixth',
  160. 'chain seventh',
  161. 'chain eighth',
  162. 'chain ninth',
  163. }
  164. local result = Collection.new()
  165. local chainDefault = args.chain or '+'
  166. for i, id in ipairs(args) do
  167. if i > 1 then
  168. result:add(args[chainNames[i - 1]] or chainDefault)
  169. end
  170. local lc = id:lower()
  171. local text = keyText[lc] or keyText[keyAlias[lc]] or id
  172. result:add(kbdPrefix .. text .. kbdSuffix)
  173. end
  174. return result:join()
  175. end
  176. local function keypress(frame)
  177. -- Called by "{{key press|...}}".
  178. -- Using the template doubles the post‐expand include size.
  179. return keyPress(frame:getParent().args)
  180. end
  181. local function press(frame)
  182. -- Called by "{{#invoke:key|press|...}}".
  183. return keyPress(frame.args)
  184. end
  185. return {
  186. keypress = keypress,
  187. press = press,
  188. }