Getting the currently pressed key

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
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Getting the currently pressed key

Post by MarekkPie » Mon Feb 13, 2012 1:00 am

So, I've noticed that one of the main complaints people have when testing someone's game is the keyboard controls are not mapped to the keys that they enjoy. (WASD/Arrows for movement; SHIFT/CTRL for crouching; etc.) So it got me to think about designing a simple remap function that would go through the list of currently bound keys and change them based on what the player inputs at that very moment (similar to the way many games/programs accomplish this).

However, it seems the only function in LOVE that gives me this information is love.keypressed/love.keyreleased, and I do not want to go overriding that inside some library/snippet, nor do I want to force someone to put in some random line of code in their love.keypressed function in order for me to see it.

Is there any current way of doing what I am asking? Here is a bit of pseudocode for perhaps a better explanation:

Code: Select all

local keys = {
        up      = "up",
        down    = "down",
        left    = "left",
        right   = "right",
        jump    = " ",
}       

local function catchKey()
        while true do
                return KEYPRESSED -- The KeyConstant that is currently being pressed
        end     
end     

function remapKeys(keys)
        local temp = deepcopy(keys)     -- Deep copy of the keys table
                                        -- so it doesn't get mapped until
                                        -- the end.

        for k,v in pairs(temp) do
                print(string.format("Please press a key to remap %s!", k))
                local catch = catchKey()
                if catch == "escape" then
                        return keys
                else
                        v = catch
                end     
        end     

        return temp
end

User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Getting the currently pressed key

Post by tentus » Mon Feb 13, 2012 1:16 am

I can't speak to the inner workings of your game, but this is kind of how I do it (pruned for leigibilty):

Code: Select all

player = class { function(self)
	self.key = {}
	self.key.left = "a"
	self.key.right = "d
end }
function player:update(dt)
	if love.keyboard.isDown(self.key.left) then
		walk(-1)
	elseif love.keyboard.isDown(self.key.right) then
		walk(1)
	end
end
Remapping the bindings is a rather lengthy series of choices in the menu which end up temporarily intercepting all menu keypresses until a key is chosen: if available, it gets mapped, if not, the menu steps back.
Kurosuke needs beta testers

User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: Getting the currently pressed key

Post by MarekkPie » Mon Feb 13, 2012 1:20 am

Yeah, i understand how to use the stuff in the table. I'm talking about the actual remap portion. Right now it seems the only way to do this is by doing:

Code: Select all

-- In main.lua

function love.keypressed(k)
        if remap then
                KEYPRESSED = k
        else    
                ...
        end     
end
But I want this to be a "plug and play" library, usable by any game that wants to have remap functionality. I suppose I could require them to put that code in their main function, but I was hoping there was some other way to do it.

User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Getting the currently pressed key

Post by tentus » Mon Feb 13, 2012 1:31 am

That's pretty much how I did it. Sorry I'm not more useful, but that solution has worked for me every time I need it.
Kurosuke needs beta testers

User avatar
The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

Re: Getting the currently pressed key

Post by The Burrito » Mon Feb 13, 2012 3:39 pm

I just have a config file that contains this:

Code: Select all

upKey = 'w'
downKey = 's'
rightKey = 'd'
leftKey = 'a'
jumpKey = ' '
and all references within the game use the key variables I defined instead of the actual key values. If the config file doesn't exist it generates one with default values. If you're making some kind of library to manage the stuff, what would be cool is if it had a decent in game way to remap the keys. Maybe set up something where there's a rebind variable, and put something in keypressed to check for that. Or even better check to see if any of the keys are undefined, something like:

Code: Select all

someFunctionThatIsRebindingRightKey()
key.right = nil
end
...
love.keypressed(k)
if not key.right then
key.right = k
end
and all internal references would use key.right instead of "d" or "right" or whatever.

Post Reply

Who is online

Users browsing this forum: No registered users and 24 guests