psychopy.org | Reference | Downloads | Github

How to check if capslock is on?

Is there any way to check if the caps lock is on?

Using PsychoPy’s standard keyboard events, we only get to tell when the 'capslock' key has been pressed, but we don’t know whether that press was turning it on or off.

So you have (at least) two options:

  • Start from the assumption that it has not been engaged, and manually keep track of its state being toggled on or off by detecting 'capslock' keypresses using the standard event.getKeys() method.
  • Use the more sophisticated ioHub keyboard event system. That can tell you whether the caps lock and num lock keys are engaged. Check out the menu item: Demos > ioHub > keyboard.py

I tried the first one, which doesn’t work, because for some reason, event.getKeys() only logs ‘capslock’ when I turn on the caps lock, but not when I turn it off. I’ll try the second one and see if it works, However, where can I access the menu?

import commands
int(commands.getoutput('xset q | grep LED')[65]) & 1

Returns 0 if caps lock is off and one if caps lock is on.

You can bitwise with 2 instead of 1 and get the numlock key instead.

best,
Allen

Hi Allen,

Saw that on StackOverflow but thought it was Linux only. Just tried and it works fine for me on OS X.

Nice.

Thank you! However this doesn’t seem to work on the Coder application of psychopy

… this doesn’t seem to work on the Coder application of psychopy

I just fired up PsychPy2 Coder v1.83.04 on Ubuntu 16.10 and it worked fine.

If you cut and pasted from my earlier message then replace the quote characters with the correct ones. Discourse disapproved of my own choice there and silently substituted its own. Hmm…seems to tolerate double quotes though, so here, just use this:

import commands
int(commands.getoutput("xset q | grep LED")[65]) & 1   

best,

Allen