How do I detect a keyboard press?

I want to be able to detect a keyboard input so that my Leonardo can react to that. The idea is that if I hit left click and enter on my keyboard at the same time, the Leonardo will have a keyboard output.

You want to use your PC keyboard to control your Arduino?
You can write a simple python program that sends all of your keystrokes, or just specific combos to your Arduino by serial.

If your PC keyboard is connected to your PC
You have to add a functionality to your PC that makes the PC send something to your Arduino Leonardo. For exaple a python-script like suggested above.

I haven't done something like that myself. I guess you can connect a PC USB-keyboard to a leonardo directly with a more special library. Your Leonardo would have to act as a USB-host towards the USB-keyboard.

If you want to have the same keyboard connected to both leonardo and PC then your leonardo would have to act as a USB-host towards the keyboard and as a USB-client towards the PC as the PC is normally the USB-host.

not very precise description. Shall the leonardo act as a USB-keyboard that is connected to a PC?

As always it is a good idea to describe what you want to do in the end.
I'm pretty sure that there are easier ways to the thing that you want to do
in the end

best regards Stefan

If you run a terminal emulator program such as Coolterm on the PC it can be configured to send each keystroke to the Arduino via a Serial link as you pres the key on the keyboard

What's the end goal here? Why do you want need to specifically get the input from the keyboard, and what will the keypress then trigger?

Yea, sorry about that. Essentially I want to use a key on my keyboard as a toggle for a mouse action from the leonardo. I'm a beginner and have no idea how to use python, would it be relatively easy to write a script like the one that was suggested?

Thank you, I'll check this out

Can you make coolterm work when you're typing anywhere, not just the command terminal?

I am not sure what you mean by this. Coolterm has to be running and you must be typing in its input window for the keystrokes to be sent to the Serial port and hence to the Arduino

This sounds very familiar from another topic on the forum within the last few weeks.. What exactly are you trying to do with the mouse action ?

Hi Disarming05,

creating a mouse-action is not a self-purpose. If the thing you want to do is not top-secret
just describe what is the mouse-move good for?

What PC-software is the "receiver" of the mouse-action?
What is this mouse-action?

  • moving the mouse? to what position? what is at this position?
  • doing a left-click? / what happens if the leftclick is done?
  • doing a double-left-click? / what happens if the double-leftclick is done?
  • doing a right-click? / what happens if the rightclick is done?
  • scrolling the mousewheel? what happends if the scrolling is done?

best regards Stefan

I meant that is there a way for me to Coolterm to work by not typing in the input window. Thank you for answering that.

I'm trying to have an autoclicker toggle when I hit the enter key so that when I hit enter and left click at the same time it will trigger the left click.

Sorry for the confusion, the goal is to have a program so that if I hit enter and left click at the same time, it will autoclick for me, but if I'm not hitting enter then it's just a single left click input.

still not clear to me describe it in this style:

I hit the following keys: ....... then I want ....................

I hit the following different keys: ....... then I want ....................

I hit the left click on my mouse and the enter key on my keyboard. Then I want the arduino leonardo to input left clicks automatically

Otherwise, nothing else should happen

Using an Arduino is quite possible, but pretty big overkill for this as it can be done on the computer in software, without the need for any additional hardware.
Check out the book: Python Automate the boring stuff. It has all the information that you need to achieve this goal.

As the first "sender" is a Windows-Computer and as the last "receiver of the actions is a Windows-Computer you can use


for this functionality.

Here is a small autohotkey-script that detects pressing the enter-key and shows a message-box
(very annoying)

This script has a small error the original script has one closing parenthesis too much

; This tells you if you hit the Enter key. Toggles the state of a boolean
; variable (BoolFlag), and sends the Enter key to your app just like
; it normally would in-game. What you do with the message box and
; the BoolFlag is up to you.
MsgBox,,,Enter Was Pressed.,2
BoolFlag := % ((!BoolFlag) & 1)

describe the functionality

in detail

that you want to have in the autohotkey-user-forum
There is a high-chance that a specialist wil answer with the wanted script.

best regards Stefan

1 Like

Sounds interesting, I'll check it out.

Thank you so much, I'll check it out and come back if there's any issues

still insufficient information:

  • how many left-clicks should be created automatically?
  • if more than one mouse left-click at which freqeuency shall the left-clicks be created?

Is there any other action that shall occur that creates another left-click or should thousands of left-clicks be created 100 clicks per second?

I'm looking for around 15 clicks per second. No other action needs to occur, just the enter and left click being hit simultaneously

Also, check the reference on Serial/Keyboard for applicable functions: