A small tool to view real-world ActivityPub objects as JSON! Enter a URL
or username from Mastodon or a similar service below, and we'll send a
request with
the right
Accept
header
to the server to view the underlying object.
{
"@context": [
"https://join-lemmy.org/context.json",
"https://www.w3.org/ns/activitystreams"
],
"type": "Page",
"id": "https://lemmy.world/post/27385536",
"attributedTo": "https://lemmy.world/u/zkfcfbzr",
"to": [
"https://programming.dev/c/python",
"https://www.w3.org/ns/activitystreams#Public"
],
"name": "Why is pynput typing incorrect output, but only on Linux?",
"cc": [],
"content": "<p>I have a rather large Python script that I use as basically a replacement for autohotkey. It uses pynput for keyboard and mouse control - and at least on Windows, it works exactly how I expect.</p>\n<p>I recently started dual-booting with Linux and have been trying to get the script to work here as well. It does work but with mixed results - in particular, I found that pynput has bizarrely wrong output for special characters, in a way thatβs both consistent and inconsistent.</p>\n<p>The simplest possible case I found that reproduces the error is this script:</p>\n<pre style=\"background-color:#ffffff;\">\n<span style=\"font-weight:bold;color:#a71d5d;\">import </span><span style=\"color:#323232;\">time\n</span><span style=\"font-weight:bold;color:#a71d5d;\">from </span><span style=\"color:#323232;\">pynput </span><span style=\"font-weight:bold;color:#a71d5d;\">import </span><span style=\"color:#323232;\">keyboard\n</span><span style=\"color:#323232;\">\n</span><span style=\"font-style:italic;color:#969896;\"># Sleep statement is just to give time to move the mouse cursor to a text input field\n</span><span style=\"color:#323232;\">time.sleep(</span><span style=\"color:#0086b3;\">2</span><span style=\"color:#323232;\">)\n</span><span style=\"color:#323232;\">\n</span><span style=\"color:#323232;\">my_kb </span><span style=\"font-weight:bold;color:#a71d5d;\">= </span><span style=\"color:#323232;\">keyboard.Controller()\n</span><span style=\"color:#323232;\">\n</span><span style=\"color:#323232;\">text </span><span style=\"font-weight:bold;color:#a71d5d;\">= </span><span style=\"color:#183691;\">'π' </span><span style=\"font-style:italic;color:#969896;\"># Eggplant emoji\n</span><span style=\"color:#323232;\">my_kb.type(text)\n</span><span style=\"color:#323232;\">\n</span><span style=\"color:#323232;\">time.sleep(</span><span style=\"color:#0086b3;\">1</span><span style=\"color:#323232;\">)\n</span><span style=\"color:#323232;\">\n</span><span style=\"color:#323232;\">text </span><span style=\"font-weight:bold;color:#a71d5d;\">= </span><span style=\"color:#183691;\">'π₯ππ€π₯' </span><span style=\"font-style:italic;color:#969896;\"># blackboard bold test\n</span><span style=\"color:#323232;\">my_kb.type(text)\n</span><span style=\"color:#323232;\">\n</span><span style=\"color:#323232;\">time.sleep(</span><span style=\"color:#0086b3;\">1</span><span style=\"color:#323232;\">)\n</span><span style=\"color:#323232;\">\n</span><span style=\"color:#323232;\">text </span><span style=\"font-weight:bold;color:#a71d5d;\">= </span><span style=\"color:#183691;\">'πππ¬π' </span><span style=\"font-style:italic;color:#969896;\"># bold test\n</span><span style=\"color:#323232;\">my_kb.type(text)\n</span></pre>\n<p>When I run that script right now, it produces the output βππ₯π₯π€π₯πππ¬πβ. And if I run it again, itβll produce the same output. And if I change the eggplant emoji to something else, like the regular character βAβ, it will still produce the same output (specifically βAπ₯π₯π€π₯πππ¬πβ). Butβ¦ If I log out and log back in, then the output changes to something else thatβs still wrong, but differently. For example, when I changed the eggplant to a regular βAβ, then relogged, the output became βAπ₯πππ₯ππππβ. And then that wrong output will keep being the same wrong output until I log out and back in again. If the test strings donβt change, then the incorrect outputs donβt change on relog - but if they do, then they do.</p>\n<p>In the larger script, errors seemed to chain together somehow - like if I produced an eggplant emoji, then tried to write blackboard bold test, I would get βπππ€πβ. This is despite verifying just before running the pynput.keyboard.Controller.type function that what it was about to type was correct. The issue also happens if I type it character-by-character with press and release functions.</p>\n<p>I am very new to Linux. Iβm on Linux Mint. Iβm running this in a python3 venv that just has pynput and two other external libraries installed. ChatGPT thinks the issue might be related to X11. The issue does not occur at all on Windows, using the exact same code. On Linux there seems to be no issues with typing regular text, just special characters.</p>\n",
"mediaType": "text/html",
"source": {
"content": "I have a rather large Python script that I use as basically a replacement for autohotkey. It uses pynput for keyboard and mouse control - and at least on Windows, it works exactly how I expect. \n\nI recently started dual-booting with Linux and have been trying to get the script to work here as well. It does work but with mixed results - in particular, I found that pynput has bizarrely wrong output for special characters, in a way that's both consistent and inconsistent. \n\nThe simplest possible case I found that reproduces the error is this script: \n\n```python\nimport time\nfrom pynput import keyboard\n\n# Sleep statement is just to give time to move the mouse cursor to a text input field\ntime.sleep(2)\n\nmy_kb = keyboard.Controller()\n\ntext = 'π' # Eggplant emoji\nmy_kb.type(text)\n\ntime.sleep(1)\n\ntext = 'π₯ππ€π₯' # blackboard bold test\nmy_kb.type(text)\n\ntime.sleep(1)\n\ntext = 'πππ¬π' # bold test\nmy_kb.type(text)\n```\n\nWhen I run that script right now, it produces the output \"ππ₯π₯π€π₯πππ¬π\". And if I run it again, it'll produce the same output. And if I change the eggplant emoji to something else, like the regular character 'A', it will still produce the same output (specifically \"Aπ₯π₯π€π₯πππ¬π\"). But... If I log out and log back in, then the output changes to something else that's still wrong, but differently. For example, when I changed the eggplant to a regular 'A', then relogged, the output became \"Aπ₯πππ₯ππππ\". And then that wrong output will keep being the same wrong output until I log out and back in again. If the test strings don't change, then the incorrect outputs don't change on relog - but if they do, then they do. \n\nIn the larger script, errors seemed to chain together somehow - like if I produced an eggplant emoji, then tried to write blackboard bold test, I would get \"πππ€π\". This is despite verifying just before running the pynput.keyboard.Controller.type function that what it was about to type was correct. The issue also happens if I type it character-by-character with press and release functions. \n\nI am very new to Linux. I'm on Linux Mint. I'm running this in a python3 venv that just has pynput and two other external libraries installed. ChatGPT thinks the issue might be related to X11. The issue does not occur at all on Windows, using the exact same code. On Linux there seems to be no issues with typing regular text, just special characters. ",
"mediaType": "text/markdown"
},
"attachment": [],
"sensitive": false,
"published": "2025-03-25T18:26:08.617221Z",
"updated": "2025-03-25T18:47:44.655567Z",
"language": {
"identifier": "en",
"name": "English"
},
"audience": "https://programming.dev/c/python",
"tag": [
{
"href": "https://lemmy.world/post/27385536",
"name": "#python",
"type": "Hashtag"
}
]
}