112

I'm currently writing a user guide containing a lot of keyboard shortcuts. I'm looking for a "keyboard" font where single characters or special keys (like tab or backspace) appear like the "real" key (i.e. with a box around; hope you get the idea).

Is there any package available for this or do I have to wrap one of the existing PS/TTF Fonts?

0

8 Answers 8

85

The keystroke package is your friend.


Update:

Sadeq's suggestion could be mimicked very inexpensively by the pgf/tikZ package.

\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shadows}

\newcommand*\keystroke[1]{%
  \tikz[baseline=(key.base)]
    \node[%
      draw,
      fill=white,
      drop shadow={shadow xshift=0.25ex,shadow yshift=-0.25ex,fill=black,opacity=0.75},
      rectangle,
      rounded corners=2pt,
      inner sep=1pt,
      line width=0.5pt,
      font=\scriptsize\sffamily
    ](key) {#1\strut}
  ;
}

\begin{document}
  The quick brown fox jumps over the lazy dog.
  \keystroke{Strg} The quick brown fox jumps over the lazy dog.
  \keystroke{Ctrl} The quick brown fox jumps over the lazy dog.
  \keystroke{Page $\uparrow$} \keystroke{Esc} \keystroke{F1}
\end{document}

enter image description here

5
  • 7
    Excellent! So no one will really have to buy a font :) You saved us $120! Feb 16, 2011 at 23:22
  • 1
    It was a great starting point. Now I am using following code (works correctly w/ sequences like Ctrl+F1): \renewcommand*\keystroke[1]{\hspace{0.2ex}\tikz[baseline=(key.base)]\node[draw,text height=1.5ex,text depth=0ex,fill=black!10,drop shadow={shadow xshift=0.2ex,shadow yshift=-0.2ex,fill=black,opacity=0.50},rectangle,rounded corners=2pt,inner sep=2.75pt,line width=0.5pt,font=\footnotesize\sffamily] (key) {#1};\hspace{0.4ex}}. \re..., because I still use keystroke package for convenient \PgUp and similar macros (you have to add \ after them if used in the middle of sentence, etc.).
    – przemoc
    Jul 7, 2011 at 14:37
  • 1
    @Thorsten Donig Is there a way to make the box have a minimum width equal to the height? (So if I'm doing the . key it isn't just a tall thin box that looks no good). Thanks Mar 22, 2015 at 10:35
  • 3
    @captainjamie: you can use the minimum width key inside the definition of keystroke. minimum width=1.2em works for me.
    – onewhaleid
    Mar 30, 2016 at 0:50
  • This is especially helpful for alphabets such as Greek where I can use: \keystroke{῏} and \keystroke{Ξ}, which might not be in a standard keystroke font.
    – Bryan M-H
    Oct 17, 2018 at 17:19
93

You can use the menukeys package which is included in texlive-full.

Example

\documentclass[a5paper,9pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{menukeys}

\begin{document}
 \section{The Manual}
    You can visualize paths \directory{/home/moose/Desktop/manual.tex}
    or menus \menu{View > Highlight Mode > Markup > LaTeX} or key
    press combinations: \keys{\ctrl + \shift + F} is for formatting
    in Eclipse.

    You can also visualize \keys{\tab}, \keys{\capslock}, \keys{\Space}, 
    \keys{\arrowkeyup} and many more.
\end{document}

Rendered

enter image description here

menukeys is included in texlive-full.

3
  • 1
    \keys{\Alt} returns a Mac-styled icon. How can I change this to something one would see in other types of keyboard? Apr 29, 2015 at 22:08
  • 6
    Answering myself. Change Mac-style icons to Windows by running \usepackage[os=win]{menukeys}. Got it from tex.stackexchange.com/q/236889/12065. Apr 29, 2015 at 22:24
  • Info for MikTex ppl: this package needs some other packages as dependencies and it might collide with other packages. For me it collided with \hypersetup options Aug 24, 2016 at 21:28
32

Another free keyboard font is Linux Biolinum Keyboard. See the following graphic for an example:

Linux Biolinum Keyboard Sample

2
  • 3
    Yes, and linux libertine. They're nicer than keystroke.
    – Leo Liu
    Feb 16, 2011 at 14:18
  • 3
    Very nice. But only available with the Libertine font. Feb 16, 2011 at 19:41
24

the ones with Libertine:

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{libertinekey}

\begin{document}

\LKeyStrg\ The quick brown fox jumps over the lazy dog. \\
\LKeyStrg\ The quick brown fox jumps over the lazy dog. \\
\LKeyPos\ \LKeyEsc \LKeyF{1}

\end{document}

enter image description here

4
  • 1
    This package seems to have been depreciated. Will you please update the answer with the latest version (I think is libertineOTF). Mar 15, 2012 at 22:32
  • 1
    why should it be deprecated? The above is an example for pdflatex and not xetex or luatex!
    – user2478
    Mar 15, 2012 at 22:57
  • 1
    Look into libertinekey.sty (in an up-to-date distribution). It is unfortunately needed to load libertine.sty from libertine-legacy.
    – Speravir
    Mar 16, 2012 at 2:29
  • 1
    I see, but that seems to be a bug. All packages of libertine-legacy should be the old one. I'll report it.
    – user2478
    Mar 16, 2012 at 5:43
14

Thorsten Donig's answer is excellent, and provides a free solution. However, if you are in need of a more advanced typographic solution, you may consider buying fonts specially designed to represent "keyboard caps."

One such font is the family of PIXymbols Shadowkey fonts. In my opinion, they are prettier (but much more expensive!)

2
  • 2
    Or use an open-source such font, like Linux Biolinum.
    – raphink
    Sep 14, 2011 at 13:27
  • @Raphink: See Philipp's answer! Sep 14, 2011 at 21:38
9

As a supplement to Philipp's answer, I'm providing a MWE for the Linux Biolinum Keyboard font. You need to compile with xelatex or lualatex. The macros are given in the libertine documentation.

\documentclass{article}
\usepackage{libertine}
\begin{document}

First hit this key sequence:\LKeyShiftX{H}

Then hit these keys one by one:
\LKey{E}\LKey{L}\LKey{L}\LKey{O}\LKeySpace\LKey{W}\LKey{O}\LKey{R}\LKey{L}\LKey{D}

And finally hit the \LKeyEnter\ key.

You should now see the phrase ``Hello world'' on your screen.

\end{document}

enter image description here

9

As an addition to Thorsten Donig's answer I modified his \keystroke command to get a similar 3D style as in the keystroke package, which he suggested.


enter image description here


\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usetikzlibrary{calc}

\newcommand*\keystroke[1]{%
  \begin{tikzpicture}[baseline=(key.base), very thin, line cap=round, black, rounded corners=0pt]%
    \node [draw, fill=white, fill opacity=1, rectangle, rounded corners=2pt, inner sep=1pt, minimum width=1.2em, font=\scriptsize\sffamily] (key) {#1\strut};

    \begin{scope}[on background layer]
      \draw [rounded corners=1pt, fill=white] ($ (key.north west) + (-2pt, 2pt) $) rectangle ($ (key.south east) + (2pt, -2pt) $);

      \fill [gray!60] ($ (key.south west) + (2pt, 0.1pt) $) -- ($ (key.south west) + (-1pt, -2pt) $)
                  -- ($ (key.south east) + (1pt, -2pt) $)  -- ($ (key.south east) + (-2pt, 0.1pt) $) -- cycle;

      \fill [gray!60] ($ (key.south east) + (-0.1pt, 2pt) $) -- ($ (key.south east) + (2pt, -1pt) $)
                  -- ($ (key.north east) + (2pt, 1pt) $)    -- ($ (key.north east) + (-0.1pt, -2pt) $) -- cycle;
    \end{scope}

    \draw ($ (key.north west) + (0.1pt, -2pt) $) -- ($ (key.north west) + (-2pt, 1pt) $);
    \draw ($ (key.north west) + (2pt, -0.1pt) $) -- ($ (key.north west) + (-1pt, 2pt) $);

    \draw ($ (key.north east) + (-0.1pt, -2pt) $) -- ($ (key.north east) + (2pt, 1pt) $);
    \draw ($ (key.north east) + (-2pt, -0.1pt) $) -- ($ (key.north east) + (1pt, 2pt) $);

    \draw ($ (key.south west) + (0.1pt, 2pt) $) -- ($ (key.south west) + (-2pt, -1pt) $);
    \draw ($ (key.south west) + (2pt, 0.1pt) $) -- ($ (key.south west) + (-1pt, -2pt) $);

    \draw ($ (key.south east) + (-0.1pt, 2pt) $) -- ($ (key.south east) + (2pt, -1pt) $);
    \draw ($ (key.south east) + (-2pt, 0.1pt) $) -- ($ (key.south east) + (1pt, -2pt) $);
  \end{tikzpicture}%
}

\begin{document}
  The quick brown fox jumps over the lazy dog.
  \keystroke{Strg} The quick brown fox jumps over the lazy dog.
  \keystroke{Ctrl} The quick brown fox jumps over the lazy dog.
  \keystroke{Page $\uparrow$} \keystroke{Esc} \keystroke{F1}
\end{document}
6

Arkandis also provides 2 keyboard fonts, suitable for, I think, French keyboards. These can be used directly with Xe/LuaTeX.

KeyboardADF

KeyboradADFNo2

Code is a hacked version of egreg's answer:

\documentclass[a4paper,12pt]{article}% ref. egreg's answer at https://tex.stackexchange.com/a/23866/
\usepackage[landscape,textwidth=239mm,textheight=480pt]{geometry}
\usepackage{fontspec,setspace}
\newfontface\keyone{KeypadADF.otf}
\newfontface\keytwo{KeypadADFNo2.otf}
\newcount\charcount
\parindent=0pt
\doublespacing
\begin{document}

  \keyone
  \chardef\highest=\XeTeXcountglyphs\font
  \offinterlineskip
  \loop
  \makebox[35mm][l]{\strut\vrule\,{\normalfont\number\charcount}\hfill \XeTeXglyph\charcount}\hskip1mm plus 1mm
  \ifnum\charcount<\numexpr\highest-1\relax
  \advance\charcount1
  \repeat
  \newpage

  \keytwo
  \charcount=0
  \chardef\highest=\XeTeXcountglyphs\font
  \offinterlineskip
  \loop
  \makebox[35mm][l]{\strut\vrule\,{\normalfont\number\charcount}\hfill \XeTeXglyph\charcount}\hskip1mm plus 1mm
  \ifnum\charcount<\numexpr\highest-1\relax
  \advance\charcount1
  \repeat

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.