User File #5481697172299767

Upload All User Files

#5481697172299767 - Lua script for lsnes: Register functions to specific keys.

lsnes_keyhook_register.lua
System: Super Nintendo Entertainment System
821 downloads
Uploaded 3/26/2013 8:26 PM by FatRatKnight (see all 245)
This is a generic helper script. Running it by itself will do nothing except fill the global variable FRK with a table of stuff in it.
As for scripts that would use it, just use FRK.registerkeypress to pick some key and some function to run whenever you press the key. You need to run the line on_keyhook = FRK.altkeyhook somewhere in your code to have it work its magic.
Need to handle like 20 different keys, and decide you'd rather avoid else ifs? Just read this sales pitch and get this script for a low price of free!
For lsnes only. Will not work in other emulators.
--[[ Simplify keyhook functions
FatRatKnight

table FRK {
   registerkeypress(key, function)
   registerkeyrelease(key, function)
   altkeyhook(same as on_keyhook)
}

These functions exist to reduce having to mess with on_keyhook functions
yourself. The functions registered are passed whatever on_keyhook is passed.

Consider the following: Suppose you want to print something. Your code:

function on_keyhook(s,t)
    if     (s == "a") and (t.last_rawval == 1) then
        print("Hello world.")
    elseif (s == "s") and (t.last_rawval == 0) then
        print("Bye bye!")
    end
end
input.keyhook("a", true)
input.keyhook("s", true)

As above, you are typing 9 lines, and the keys you want to interact with are in
two places. The first time to identify which key was affected, and the second
time to tell lsnes to call us. If you decide you didn't want to use that key,
you have two places to change.

In comparison, if you use this file, it might be simply as follows:

on_keyhook= FRK.altkeyhook
FRK.registerkeypress("a",function()
    print("Hello world.")
end)
FRK.registerkeyrelease("s",function()
    print("Bye bye.")
end)

Fewer lines (7) are typed, and you only need to change one part of your code in
order to add, remove, or modify a key you wish to use. Furthermore, you don't
ever have to think about input to on_keyhook, as the code here does that.
]]--

FRK= FRK or {}

FRK.KeyPress=   {}
FRK.KeyRelease= {}

--*****************************************************************************
function FRK.registerkeypress(key,fn)
--*****************************************************************************
-- key - string. Which key do you wish to bind?
-- fn  - function. To execute on key press. False or nil removes it.
-- Return value: The old function previously assigned to the key.

    local OldFn= FRK.KeyPress[key]
    FRK.KeyPress[key]= fn
    input.keyhook(key,type(fn or FRK.KeyRelease[key]) == "function")
    return OldFn
end

--*****************************************************************************
function FRK.registerkeyrelease(key,fn)
--*****************************************************************************
-- key - string. Which key do you wish to bind?
-- fn  - function. To execute on key release. False or nil removes it.
-- Return value: The old function previously assigned to the key.

    local OldFn= FRK.KeyRelease[key]
    FRK.KeyRelease[key]= fn
    input.keyhook(key,type(fn or FRK.KeyPress[key]) == "function")
    return OldFn
end

--*****************************************************************************
function FRK.altkeyhook(s,t)
--*****************************************************************************
-- s,t - input expected is identical to on_keyhook input. Also passed along.
-- You may set by this line: on_keyhook = FRK.altkeyhook
-- Only handles keyboard input. If you need to handle other inputs, you may
-- need to have your own on_keyhook function to handle that, but you can still
-- call this when generic keyboard handling is desired.

    if     FRK.KeyPress[s]   and (t.last_rawval == 1) then
        FRK.KeyPress[s](s,t)
    elseif FRK.KeyRelease[s] and (t.last_rawval == 0) then
        FRK.KeyRelease[s](s,t)
    end
end