ID:158509
 
Is there any way to detect when a key, ANY key, has been pressed, and then report that somewhere in the game without having to override every key in the interface macros manually?
Foomer wrote:
Is there any way to detect when a key, ANY key, has been pressed, and then report that somewhere in the game without having to override every key in the interface macros manually?

Not that I know of. Depending on what you mean by manually... You could use a code loop to auto-create a macro for each key.
You could try using javascript like that old key handler library did, but I'm not sure of the exact method. I believe it required keeping constant focus on an invisible browser window.
In response to Falacy
Falacy wrote:
You could use a code loop to auto-create a macro for each key.

How would you go about doing that?
In response to Foomer
mob/verb/KeyPressed(var/Key as null|text)
src<<"You pressed [Key]"

mob/proc/MacroAll()
for(var/i=97;i<=122;i++)
var/Key=ascii2text(i)
winset(src,"KeyPressed[Key]","parent=macro;name=[Key];command='KeyPressed [Key]'")


That'll get all the basic letters a-z.
In response to Falacy
Falacy wrote:
> mob/verb/KeyPressed(var/Key as null|text)
> src<<"You pressed [Key]"
>
> mob/proc/MacroAll()
> for(var/i=97;i<=122;i++)
> var/Key=ascii2text(i)
> winset(src,"KeyPressed[Key]","parent=macro;name=[Key];command='KeyPressed [Key]'")
>

That'll get all the basic letters a-z.

Not exactly control over the entire keyboard, especially Shift+Tab kind of stuff... Makes it kind of hard to route every key press through a certain datum.
In response to Foomer
Foomer wrote:
Not exactly control over the entire keyboard, especially Shift+Tab kind of stuff...

Special keys will have to be added (like tab) but you can setup the shift/ctrl/up thing by setting the name to something like SHIFT+[Key]
In response to Falacy
That makes for 864 possible combination when you include the alphanumeric keys along with everything in that extra macros page (Tab, numpad, etc), with eight possible combinations of Alt, Ctrl and Shift each.

So it looks like I'd have to specify a macro interface to modify, add all possible macros at the start of the game, and along with that, create proc definitions for all 864 possible procs, since we're using Call()() here as having a switch() would be atrocious.

BTW, anyone know how to tell winset() to create new macros using associative params? Eg:

var/list/macros = list()
for(var/key in keys)
macros["[key]"] = "[key]"
macros["[macro].[key].name"] = "[key]"
macros["[macro].[key].command"] = "CallMacro [key]"
winset(usr, null, list2params(macros))
In response to Foomer
You can just call one verb and use the key as an argument.
In response to Garthor
Garthor wrote:
You can just call one verb and use the key as an argument.

I'm attempting to use this in a library and it would be much more appealing to have people call datum.ShiftA() and a few other procs than to have them make their own proc with a switch statement which must account for every key stroke that they want to use.
In response to Foomer
switch(X)
if("A")
if("B")
if("C")


or

proc
A()
B()
C()


I'm not seeing any gains in ease of use or sanity, here. They seem to be BASICALLY the same, only the first one is more flexible.
In response to Garthor
This is a moot point. Go back and answer the question in [link] if you want to be helpful. :P
In response to Garthor
Something easier could be using hascall() and call()(), which would allow you to define the procs and not have to worry about making them all, so long as you decide definitely on a naming convention for the procs.
In response to Popisfizzy
Yeah, that's a better way to do it.