# How can I find out the state of capslock?

**URL:** <https://www.ruby-forum.com/t/how-can-i-find-out-the-state-of-capslock/149956>\
**Category:** wxRuby\
**Created:** [October 1, 2008, 4:08pm UTC](https://www.ruby-forum.com/t/how-can-i-find-out-the-state-of-capslock/149956 "2008-10-01T16:08:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rossgoodell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@rossgoodell](https://www.ruby-forum.com/u/rossgoodell)\
**Post date:** [October 1, 2008, 4:08pm UTC](https://www.ruby-forum.com/t/how-can-i-find-out-the-state-of-capslock/149956/1 "2008-10-01T16:08:33Z")

</div>

Although I can use KeyEvent#shift\_down to find out if the shift key was  
held down when a key is pressed, I don’t know how to find out if the  
capslock key is in effect (the capslock key was pressed the capslock LED  
is on, and other programs are treating an ‘a’ keypress as an ‘A’). I  
could catch capslock keypresses by checking evt.get\_key\_code for 311,  
but don’t know what the starting capslock state is since it might have  
been set when the user was in another program. Can someone tell me how  
to find out what what the capslock state is?

Thanks, Ross

---

<div class="post-metadata">

**Author:** ![rossgoodell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@rossgoodell](https://www.ruby-forum.com/u/rossgoodell)\
**Post date:** [October 1, 2008, 4:45pm UTC](https://www.ruby-forum.com/t/how-can-i-find-out-the-state-of-capslock/149956/2 "2008-10-01T16:45:15Z")

</div>

Hi Ross

Ross Goodell wrote:

> Although I can use KeyEvent#shift\_down to find out if the shift key was  
> held down when a key is pressed, I don’t know how to find out if the  
> capslock key is in effect (the capslock key was pressed the capslock LED  
> is on, and other programs are treating an ‘a’ keypress as an ‘A’). I  
> could catch capslock keypresses by checking evt.get\_key\_code for 311,  
> but don’t know what the starting capslock state is since it might have  
> been set when the user was in another program. Can someone tell me how  
> to find out what what the capslock state is?  
> Wx::get\_key\_state(Wx::K\_CAPITAL)

Wx::get\_key\_state is a global method that returns true/false depending  
on whether the key with the specified code is currently depressed.  
Specially, for the toggle “lock” keys num / caps / scroll (whose integer  
key codes are Wx::K\_NUMLOCK, Wx::K\_CAPITAL and Wx::K\_SCROLL) it instead  
returns true if the relevant lock is currently on.

a

---

<div class="post-metadata">

**Author:** ![rossgoodell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@rossgoodell](https://www.ruby-forum.com/u/rossgoodell)\
**Post date:** [October 1, 2008, 5:18pm UTC](https://www.ruby-forum.com/t/how-can-i-find-out-the-state-of-capslock/149956/3 "2008-10-01T17:18:07Z")

</div>

Alex F. wrote:

> Hi Ross
> 
> Ross Goodell wrote:
> 
> > Although I can use KeyEvent#shift\_down to find out if the shift key was  
> > held down when a key is pressed, I don’t know how to find out if the  
> > capslock key is in effect (the capslock key was pressed the capslock LED  
> > is on, and other programs are treating an ‘a’ keypress as an ‘A’). I  
> > could catch capslock keypresses by checking evt.get\_key\_code for 311,  
> > but don’t know what the starting capslock state is since it might have  
> > been set when the user was in another program. Can someone tell me how  
> > to find out what what the capslock state is?  
> > Wx::get\_key\_state(Wx::K\_CAPITAL)
> 
> Wx::get\_key\_state is a global method that returns true/false depending  
> on whether the key with the specified code is currently depressed.  
> Specially, for the toggle “lock” keys num / caps / scroll (whose integer  
> key codes are Wx::K\_NUMLOCK, Wx::K\_CAPITAL and Wx::K\_SCROLL) it instead  
> returns true if the relevant lock is currently on.
> 
> a

Thanks Alex for replying so quickly. It worked beautifully.

Ross
