Real Keyboard Access?

Am I right that there is essentially no access to keypress information on the real (Magic?) keyboard? I was hoping to use it for game controls.

Thanks!

@RonJeffries this is something I’m keen on doing really soon (and might be a “Pro” feature if Codea switches over before I do that)

Maybe you can help me make a decision on the API? I’ve been considering the following:

##Global Functions

function keyPressed(key, modifiers)
    if key == Keys.W then
        -- 'W' pressed
    end
end

function keyReleased(key, modifiers)
    if key == Keys.W then
        -- 'W' released
    end

    if key == Keys.A and (modifiers & Keys.shift) then
        -- 'A' released while holding shift
    end
end

These are mode “Codea like” but I find them a little ugly due to the fact that Lua doesn’t have a switch statement. So you’ll end up with a big if / elseif chain

##Closures

Keys.A.pressed = function(modifiers) 
    if modifiers & keys.shift then
        -- A pressed with Shift modifier
    end
end

Keys.A.released = function(modifiers) 
    if modifiers & keys.shift then
        -- A pressed with Shift modifier
    end
end

I like this better, but it’s a little less obvious for people new to programming.


Also you’ll notice I’m using Lua’s bitwise operators for the modifier keys. I’m not convinced that bitwise is good syntax here, it doesn’t read well but it’s part of the language and I feel like I should use it.

The alternative is:

modifiers.contains(Keys.shift) instead of modifiers & Keys.shift

For multiple modifiers this looks like:

modifiers.contains(Keys.shift, Keys.ctrl) and

modifiers & (Keys.shift | Keys.ctrl)


I’d possibly like to support both syntaxes. As I would much prefer the second but can see how the first is very “Codea”

I like the Keys.A.pressed style as well. It is a bit unfamiliar although I think it is perfectly good Lua, it’s a bit advanced. So I guess it might be desirable to do both, or failing that, just the first.

The modifiers are a rathole however one does them, I fear. The tricky one may be the “or” case, shift or ctrl. Either way it gets messy and confusing, I fear.

I thought briefly about returning a key-pressed table (for the individual key) ala touch, but that doesn’t really make things better at first glance.

I think the bit mode for modifiers is probably closer to the spirit of Lua, which is pretty close to the metal in my view. Given that, one could provide examples of how to create more sophisticated structures like Keys directly in Lua, driven from the simple event.

@RonJeffries oh I realised we already solved this for the gesture type that comes with the new hover and scroll callbacks

We will have boolean fields on modifiers that are set true or false depending on whether the key is active

Keys.A.pressed = function(modifiers) 

    if modifiers.ctrl and modifiers.alt then
        -- A pressed with ctrl and alt
    end

    if modifiers.ctrl or modifiers.shift then
        -- A pressed with either ctrl or alt
    end

end

The bitwise method doesn’t sit well with me because OR | often means AND in the sense that you want to detect both of those keys simultaneously

yes. as shown here, that looks super to me!