Is there any way to check for CAPS LOCK being activated?

Started by GarageGothic, Fri 17/06/2005 10:58:22

Previous topic - Next topic

GarageGothic

I was wondering if there's any way to check for Caps Lock being on/off (like you can check for Shift being pressed)?

naltimari

Quote from: GarageGothic on Fri 17/06/2005 10:58:22
I was wondering if there's any way to check for Caps Lock being on/off (like you can check for Shift being pressed)?

I found out that IsKeyPressed(426) detects if the user pressed CAPS LOCK, but I guess there is no way to know whether if it is activated or not.

Lee

I'm not sure if this would work or not but could you create a routine to monitor the CAPS KEY being pressed (426) and use it to set a global f ???lag that says weather the CAPS are set or reset.

DoorKnobHandle

...which would work great unless the user starts the program with CAPS already on or unless the user decides to switch the CAPS-state in a game-situation where no key-presses are detected (in a Wait()-period for instance) - then the flag would be !true... ;)

naltimari

Quote
...which would work great unless the user starts the program with CAPS already on

Exactly. The ideal test for CAPS LOCK should be like mouse.Mode, or something, otherwise the state of CAPS LOCK is not accurate.

Besides that, IsKeyPressed() returns true only if the key is being pressed at the moment, so if you test for it occasionally, you might not get it, and if you test for it in rep_ex_always, you might risk setting the flag many times.

This naïve approach definetely does not work:

Code: ags

function repeatedly_execute_always()
{
  if (!capslock && IsKeyPressed(426))
    capslock = true;
  else if (capslock && IsKeyPressed(426))
    capslock = false;
}


because since it is executed many times per second, the flag is unset right after it was set, should the user hold the key down for more than 1/40 of a second... and most of us are not THAT fast to hold it for a shorter time.

Khris

A quick way to sort this out is to only set the flag after the key is released.

naltimari

Quote from: KhrisMUC on Fri 04/04/2008 20:57:22
A quick way to sort this out is to only set the flag after the key is released.

Hmm. It does not solve the 'initial state problem', but it's a good idea. How about this:

Code: ags

  if (IsKeyPressed(426))
    capsdown = true;
  else if (capsdown) {
    capslock = !capslock;
    capsdown = false;
  }


It works, but uses two 'flags'... any other ideas?

Radiant

You can possibly detect if the caps lock is on by requiring the user to press the 'A' key at some point, and reading whether it returns 'a' or 'A', and whether either shift key is held.

naltimari

Quote from: Radiant on Fri 04/04/2008 23:18:10
You can possibly detect if the caps lock is on by requiring the user to press the 'A' key at some point, and reading whether it returns 'a' or 'A', and whether either shift key is held.

Actually, this won't work, since the keycodes for lowercase and uppercase are the same (as per the current manual).

Radiant

Quote from: naltimari on Sat 05/04/2008 00:23:04
Actually, this won't work, since the keycodes for lowercase and uppercase are the same (as per the current manual).

Yes, but you can still use input boxes, both the one from the function and the one on GUIs.

OneDollar

But then you can't tell if they pressed the shift key to get a capital or not?

monkey0506

Certainly there's a way for the system to register whether CAPS has been activated, so as to know whether to produce the lower or upper case characters whilst typing. So the question would become whether CJ could implement a hook into the system's check and return the result to AGS.

Clearly there's no built-in method in AGS yet to do this, but I think naltimari's solution would probably be a good workaround albeit no way of assuring the initial state. Perhaps in these sequences where you required a check of CAPS you could have a 'calibration' feature?

BTW naltimari, that's a smooth workaround. That may solve a problem I've been having...once I have a computer again! :-\

TwinMoon

Good to see you're still debating the nuts and bolts of AGS, monkey_05_06!

It's odd that the keycodes for a and A are the same.  I would think that A=65 and a=97, but AGS reads both keycodes as A.
Clearly when typing text into a textbox a and A are read as different characters. You might do something with this to check whether the capslock is pressed or not. I don't know if IsKeyPressed can be used when a text box is displayed.

The only other solution is to ask the player whether the capslock is on or off at the beginning ;)

Radiant

Quote from: TwinMoon on Sat 05/04/2008 20:29:42
It's odd that the keycodes for a and A are the same.  I would think that A=65 and a=97, but AGS reads both keycodes as A.
They're the same because they're key codes, not character codes. That's also why the left shift has a different code than the right shift, even though they're functionally the same.

TwinMoon

Hm.

Well, technically a is Shift-A. Ctrl-A also has a keycode.

naltimari

Quote from: Pumaman on Wed 30/01/2008 21:55:37
Changes in 3.0.1 Final:
* Added System.NumLock/CapsLock/ScrollLock properties

Wohoo!! Problem solved, I guess... Thanks, CJ!

monkey0506

Heh,  CJ is a sneaky one indeed. Without even posting he corrected this issue behind all of our backs. :=

Makes AGS somewhat like a detective game...finding all the secrets hidden within! ;D

SMF spam blocked by CleanTalk