Use the bold typewriter font from lmodern for keywords in listings but keep Computer Modern fonts in the rest of document in LaTeX

boldlistingslmoderntypewriter

I am writing a document in LaTeX using the standard Computer Modern fonts. The text is mainly in Russian but it includes some listings. I prefer to typeset listings using the standard typewriter font (\ttfamily). It would be really nice if I could use a bold typewriter font for keywords in listings.

If my document was in English only, I would use the bold typewriter font from lmodern:

\usepackage{lmodern}
\usepackage{listings}
\lstset{%
basicstyle=\ttfamily\fontseries{l}\selectfont,
keywordstyle=\ttfamily\fontseries{b}\selectfont
}

However, this cannot be used in my case because lmodern does not support Cyrillic. But what is (theoretically) good is that all the keywords in listings contain Latin letters only. Consequently, my idea is to use the bold typewriter lmodern font (i.e., \ttfamily\fontseries{b}\selectfont) for keywords in listings and to keep Computer Modern fonts in the rest of my document (including the non-keyword entries in listings which contain strings and comments in Russian). Unfortunately, I do not know how to do it. It seems that when I load lmodern package, it changes the fonts throughout the entire document and I lose much of font formatting because of Russian letters. Could someone please help me with this?

Importantly, I need to use LaTeX only (not XeLaTeX, not LuaLaTeX). And I should probably note that the bold typewriter font specifically from lmodern is not really important, I just like how it looks.

Best Answer

Thanks to David Carlisle and egreg, I came up with this solution:

\documentclass[12pt]{article}

\usepackage[T1,T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english, russian]{babel}
\usepackage{listings}
\lstset{%
basicstyle=\ttfamily,
keywordstyle=\fontencoding{T1}\fontfamily{lmtt}\bfseries
}

\begin{document}

Русский текст. \textbf{Жирный шрифт.}

\begin{lstlisting}[language=Python]
def func(s):
    return s
\end{lstlisting}

\end{document}

Output