Skip to content

MkKeys

Show source on GitHub

Node to display Keyboard keys.

Description

Mostly used to explain shortcuts / commands for the keyboard.

Example: Single key

Jinja

{{ "M" | MkKeys }}

Python

MkKeys(['m'])

M

++m++
<p><span class="keys"><kbd class="key-m">M</kbd></span></p>

Example: Multiple keys

Jinja

{{ "M+k+K+e+y+s" | MkKeys }}

Python

MkKeys(['m', 'k', 'k', 'e', 'y', 's'])

M+K+K+E+Y+S

++m+k+k+e+y+s++
<p><span class="keys"><kbd class="key-m">M</kbd><span>+</span><kbd class="key-k">K</kbd><span>+</span><kbd class="key-k">K</kbd><span>+</span><kbd class="key-e">E</kbd><span>+</span><kbd class="key-y">Y</kbd><span>+</span><kbd class="key-s">S</kbd></span></p>

Example: Modifiers

Jinja

{{ "Ctrl+A" | MkKeys }}

Python

MkKeys(['ctrl', 'a'])

Ctrl+A

++ctrl+a++
<p><span class="keys"><kbd class="key-control">Ctrl</kbd><span>+</span><kbd class="key-a">A</kbd></span></p>

Example: As list

Jinja

{{ ["Alt", "X"] | MkKeys }}

Python

MkKeys(['alt', 'x'])

Alt+X

++alt+x++
<p><span class="keys"><kbd class="key-alt">Alt</kbd><span>+</span><kbd class="key-x">X</kbd></span></p>

Bases: MkNode

__init__

__init__(keys: str | list[str], **kwargs: Any)

Parameters:

Name Type Description Default
keys str | list[str]

keys to display

required
kwargs Any

Keyword arguments passed to parent

{}
Name Children Inherits
MkNode
mknodes.basenodes.mknode
Base class for everything which can be expressed as Markup.
graph TD
  94721311202288["mkkeys.MkKeys"]
  94721308848336["mknode.MkNode"]
  94721311766592["node.Node"]
  140564252373184["builtins.object"]
  94721308848336 --> 94721311202288
  94721311766592 --> 94721308848336
  140564252373184 --> 94721311766592
/home/runner/work/mknodes/mknodes/mknodes/basenodes/mkkeys/metadata.toml
[metadata]
icon = "fa6-regular:keyboard"
name = "MkKeys"

[requirements.extension."pymdownx.keys"]

[examples.single_key]
title = "Single key"
jinja = """
{{ "M" | MkKeys }}
"""

[examples.multiple_keys]
title = "Multiple keys"
jinja = """
{{ "M+k+K+e+y+s" | MkKeys }}
"""

[examples.modifiers]
title = "Modifiers"
jinja = """
{{ "Ctrl+A" | MkKeys }}
"""

[examples.as_list]
title = "As list"
jinja = """
{{ ["Alt", "X"] | MkKeys }}
"""

[output.markdown]
template = """
++{{ node.keys | join("+") }}++
"""
mknodes.basenodes.mkkeys.MkKeys
class MkKeys(mknode.MkNode):
    """Node to display Keyboard keys.

    Mostly used to explain shortcuts / commands for the keyboard.
    """

    REQUIRED_EXTENSIONS = [resources.Extension("pymdownx.keys")]
    ICON = "fontawesome/regular/keyboard"
    ATTR_LIST_SEPARATOR = ""

    def __init__(
        self,
        keys: str | list[str],
        **kwargs: Any,
    ):
        """Constructor.

        Args:
            keys: keys to display
            kwargs: Keyword arguments passed to parent
        """
        super().__init__(**kwargs)
        match keys:
            case str():
                self.keys = keys.lower().split("+")
            case list():
                self.keys = [i.lower() for i in keys]

    def _to_markdown(self) -> str:
        key_str = "+".join(self.keys)
        return f"++{key_str}++"