How can I typeset the math symbol with a circle and a rectangle attached to it?

Without additional guidance, I made it based on \circ and defined it as \mathbin.

For a version that only works in the regular math size:

\documentclass{article}
\usepackage{xcolor}
\usepackage{stackengine}
\newcommand\mathkey{%
  \mathbin{\stackinset{r}{-.98pt}{c}{.3pt}{%
    \def\stackalignment{l}%
    \stackon[-.85pt]{\rule{2pt}{1.2pt}}{\textcolor{white}{\rule{1.65pt}{0.45pt}}}%
  }{$\circ$}\kern1.2pt}%
}
\begin{document}
$A\mathkey B$
\end{document}

enter image description here

For the version that works across math styles:

\documentclass{article}
\usepackage{xcolor}
\usepackage{stackengine,scalerel}
\newcommand\mathkey{%
  \mathbin{\scalerel*{\stackinset{r}{-.98pt}{c}{.3pt}{%
    \def\stackalignment{l}%
    \stackon[-.85pt]{\rule{2pt}{1.2pt}}{\textcolor{white}{\rule{1.65pt}{0.45pt}}}%
  }{$\circ$}\kern1.2pt}{\circ}}%
}
\begin{document}
$A\mathkey B$

$\scriptstyle A\mathkey B$

$\scriptscriptstyle A\mathkey B$
\end{document}

enter image description here

Note that the final \circ in the definition of \mathkey defines the vertical footprint of the \mathkey symbol. Thus, one could change that to something else, let's say X, in order to enlarge the resulting \mathkey:

\documentclass{article}
\usepackage{xcolor}
\usepackage{stackengine,scalerel}
\newcommand\mathkey{%
  \mathbin{\scalerel*{\stackinset{r}{-.98pt}{c}{.3pt}{%
    \def\stackalignment{l}%
    \stackon[-.85pt]{\rule{2pt}{1.2pt}}{\textcolor{white}{\rule{1.65pt}{0.45pt}}}%
  }{$\circ$}\kern1.2pt}{X}}%
}
\begin{document}
$A\mathkey B$

$\scriptstyle A\mathkey B$

$\scriptscriptstyle A\mathkey B$
\end{document}

enter image description here


Prepare a keyhole.mp file containing

beginfig(1);
w=18pt;
h=10pt;
o=1pt;
r=h/2;
pickup pencircle scaled 1pt;
lft z1=(o,r);
top z2=(r+o,h);
bot z4=(r+0,0);
rt z3=(2r+o,h/2);
path circlepart;
circlepart=z1..z2..z3..z4..cycle;
z5=point 2-1/4 of circlepart;
z6=(w-o,y5);
z7=(w-o,y8);
z8=point 2+1/4 of circlepart;
draw z1..z2..z5--z6--z7--z8..z4..cycle;
endfig;
end.

This is inspired from “lightbulb” in the Comprehensive List of LaTeX symbols.

Run mptopdf keyhole.mp, which will produce a file with name keyhole-1.pdf.

Now the LaTeX test file:

\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}

\DeclareRobustCommand{\keyhole}{%
  \mathrel{\text{\includegraphics[height=\fontcharht\font`X]{keyhole-1}}}%
}

\begin{document}

$A\keyhole B_{\keyhole}$

\end{document}

It would be possible to define different figures with different line thickness for the smaller sizes.

enter image description here