Detect if capslock is toggled on ?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
KayleMaster
Party member
Posts: 234
Joined: Mon Aug 29, 2016 8:51 am

Detect if capslock is toggled on ?

Post by KayleMaster »

Title says it all.
My code is in love.keypressed(key):

Code: Select all

if inputObj ~= nil then
   		if isLetter(key)==1 or isDigit(key)==1 then
   			if (love.keyboard.isDown("lshift") or love.keyboard.isDown("rshift") or key=="capslock") and #(inputObj.input_string)<24 then  				
   				inputObj.input_string = inputObj.input_string ..  numpadCheck( toUpper(key))
   			else
   				if #(inputObj.input_string)<24 then inputObj.input_string = inputObj.input_string ..  numpadCheck(key) end
   			end
   		elseif key == "backspace" or key == "delete" then
        	inputObj.input_string = string.sub(inputObj.input_string, 1, #(inputObj.input_string)-1)
   		end
    end
if key == "capslock" doesn't work, didn't expect it to, but worth a shot I guess. It says that Caps-on is a key-press, what does that mean? How do I check it?
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Detect if capslock is toggled on ?

Post by raidho36 »

Caps mode is OS-dependant, plus there are a few more text modes besides caps. The point is, you shouldn't care, and just take the input as it is. Then, after you're finished, you can do some text processing.
KayleMaster
Party member
Posts: 234
Joined: Mon Aug 29, 2016 8:51 am

Re: Detect if capslock is toggled on ?

Post by KayleMaster »

But the input has to be case-sensitive and I don't want to remove this functionality which is pretty much built in Windows from the user.
If I have to, I will check on which OS the user is (I'm planning a release for Windows and Linux only for now).
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Detect if capslock is toggled on ?

Post by bartbes »

It sounds like you want to be using love.textinput instead. That automatically deals with caps lock, keyboard layouts, and even multi-key input sequences.
KayleMaster
Party member
Posts: 234
Joined: Mon Aug 29, 2016 8:51 am

Re: Detect if capslock is toggled on ?

Post by KayleMaster »

I don't want any special symbols in my string so I just wrote my own that accepts A-Z, a-z, 0-9.
How would I handle that with textinput?
Lucyy
Citizen
Posts: 51
Joined: Thu Oct 15, 2015 6:57 am
Contact:

Re: Detect if capslock is toggled on ?

Post by Lucyy »

The love.textinput function has a variable with the character in it. You could check that

Code: Select all

function love.textinput(t)
	if t == "a" then
		
	end
end
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Detect if capslock is toggled on ?

Post by raidho36 »

KayleMaster wrote: Mon Apr 17, 2017 3:47 pm I don't want any special symbols in my string so I just wrote my own that accepts A-Z, a-z, 0-9.
How would I handle that with textinput?
You accept all text input and then do string match with a pattern that only allows those characters:

Code: Select all

local output = input:gsub ( '[^A-Za-z0-9]', '' )
You can also do this on per-character basis, if you accept one character at a time. Just make sure to make some indication that character was not accepted.
User avatar
whiteland92
Prole
Posts: 12
Joined: Fri Jul 04, 2014 1:30 am

Re: Detect if capslock is toggled on ?

Post by whiteland92 »

Or you can filter out non alphanumeric when they type.

Code: Select all

function love.textinput(t)
	if t:match("%w") then
           --"It's alphanumeric only"
        end
end
KayleMaster
Party member
Posts: 234
Joined: Mon Aug 29, 2016 8:51 am

Re: Detect if capslock is toggled on ?

Post by KayleMaster »

Oh nice, didn't know about pattern matching yet.
Is the parameter of textinput a string, a key, or something else?
User avatar
zorg
Party member
Posts: 3449
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Detect if capslock is toggled on ?

Post by zorg »

KayleMaster wrote: Mon Apr 17, 2017 8:56 pm Oh nice, didn't know about pattern matching yet.
Is the parameter of textinput a string, a key, or something else?
It's the UTF-8 encoded character that was pressed on the keyboard.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 4 guests