+ Reply to Thread
Page 1 of 6 123 ... LastLast
Results 1 to 10 of 52

Thread: E2 - Input Support

  1. #1
    Wire Sofaking ZeikJT's Avatar
    Join Date
    Dec 2008
    Location
    California
    Posts
    1,391

    Default E2 - Input Support

    I'm surprised no has done this yet, what with everyone saying it's sooooo needed. So, here it is.

    I coded both aspects of key listening, the limited serverside is there, so if you need something really simple then use that.

    If you need more control use the clientside listening, but keep it down to a minimum okay?

    The system is using the same ascii codes the keyboard does. I've included some helper functions to go from the gmod wiki key codes to the keyboard ones. All the info is on the wiki.



    Here's the updated code:
    Code:
    /******************************************************************************\
      Input support v1.2
    \******************************************************************************/
    
    local IsListeningForKey,cIsTriggeringForKey,IsTriggeringForKey,KeyTable,Times2 = {}, {}, {}, {}, {}
    
    if CLIENT then
    cIsTriggeringForKey,IsTriggeringForKey,KeyTable,Times2 = nil, nil, nil, nil
    IsListeningForKey.chat = {true}
    
    usermessage.Hook("e2keystartlistening", function( um )
    	local key = 0
    	repeat
    		key = um:ReadShort()
    		if key>130 then
    			key = key-131
    			IsListeningForKey.chat[1] = key~=0
    			return
    		elseif key>0 then
    			IsListeningForKey[key] = 0
    		else
    			IsListeningForKey[-key] = nil
    		end
    	until key==0
    end)
    
    hook.Add("CalcView", "e2keythink", function()
    	if (WIRE_CLIENT_INSTALLED) then
    		if IsListeningForKey.chat[1] and IsListeningForKey.chat[2] then return end
    		local list = ""
    		for k,v in pairs(IsListeningForKey) do
    			if k!="chat" then
    				if(input.IsKeyDown(k) && v!=1) then
    					// The key has been pressed
    					IsListeningForKey[k]=1
    					RunConsoleCommand("e2keystate",k,1)
    				elseif(!input.IsKeyDown(k) && v!=0) then 
    					// The key has been released
    					IsListeningForKey[k]=0
    					RunConsoleCommand("e2keystate",k,0)
    				end
    			end
    		end
    	end
    end)
    
    hook.Add("StartChat", "e2chatcheckinitiated", function()
    	IsListeningForKey.chat[2] = true
    	if IsListeningForKey.chat[1] then
    		local list = ""
    		for k,v in pairs(IsListeningForKey) do
    			if k!="chat" and v==1 then
    				RunConsoleCommand("e2keystate",k,0)
    			end
    		end
    	end
    	return false
    end)
    
    hook.Add("FinishChat", "e2chatcheckfinished", function()
    	IsListeningForKey.chat[2] = false
    	return
    end)
    
    elseif SERVER then
    
    concommand.Add( "e2keystate", function( player, command, args )
    	IsListeningForKey[player] = IsListeningForKey[player] or {}
    	args[1],args[2] = tonumber(args[1]), tonumber(args[2])
    	IsListeningForKey[player][args[1]] = args[2]
    	if cIsTriggeringForKey[player] and cIsTriggeringForKey[player][args[1]] then
    		for k,_ in pairs(cIsTriggeringForKey[player][args[1]])do
    			if k and k:IsValid() then
    	            			k:Execute()
    			else
    				cIsTriggeringForKey[player][args[1]][k] = nil
    			end
    		end
    	end
    end)
    
    for i=1,26 do
    	if i<10 then
    		KeyTable["..(i).."] = i+1
    	end
    	if i<11 then
    		KeyTable["pad"..i-1] = i+36
    	end
    	if i<13 then
    		KeyTable["f"..(i)] = i+91
    	end
    	KeyTable[string.char(i+96)] = i+10
    end
    KeyTable["pad/"] = 47
    KeyTable["pad*"] = 48
    KeyTable["pad-"] = 49
    KeyTable["pad+"] = 50
    KeyTable["padenter"] = 51
    KeyTable["pad."] = 52
    KeyTable["["] = 53
    KeyTable["]"] = 54
    KeyTable[";"] = 55
    KeyTable["'"] = 56
    KeyTable["`"] = 57
    KeyTable[","] = 58
    KeyTable["."] = 59
    KeyTable["/"] = 60
    KeyTable["\\"] = 61
    KeyTable["-"] = 62
    KeyTable["="] = 63
    KeyTable.enter = 64
    KeyTable[" "] = 65
    KeyTable.backspace = 66
    KeyTable.tab = 67
    KeyTable.capslock = 68
    KeyTable.numlock = 69
    KeyTable.escape = 70
    KeyTable.scrolllock = 71
    KeyTable.insert = 72
    KeyTable.delete = 73
    KeyTable.home = 74
    KeyTable["end"] = 75
    KeyTable.pageup = 76
    KeyTable.pagedown = 77
    KeyTable["break"] = 78
    KeyTable.lshift = 79
    KeyTable.rshift = 80
    KeyTable.lalt = 81
    KeyTable.ralt = 82
    KeyTable.lcontrol = 83
    KeyTable.rcontrol = 84
    KeyTable.lwin = 85
    KeyTable.rwin = 86
    KeyTable.app = 87
    KeyTable.up = 88
    KeyTable.left = 89
    KeyTable.down = 90
    KeyTable.right = 91
    KeyTable["capslocktoggle"] = 104
    KeyTable["numlocktoggle"] = 105
    KeyTable["scrolllocktoggle"] = 106
    KeyTable["xup"] = 107
    KeyTable["xdown"] = 108
    KeyTable["xleft"] = 109
    KeyTable["xright"] = 110
    KeyTable["xstart"] = 111
    KeyTable["xback"] = 112
    KeyTable["xstick1"] = 113
    KeyTable["xstick2"] = 114
    KeyTable["xa"] = 115
    KeyTable["xb"] = 116
    KeyTable["xx"] = 117
    KeyTable["xy"] = 118
    KeyTable["xblack"] = 119
    KeyTable["xwhite"] = 120
    KeyTable["xltrigger"] = 121
    KeyTable["xrtrigger"] = 122
    KeyTable["xstick1up"] = 123
    KeyTable["xstick1down"] = 124
    KeyTable["xstick1left"] = 125
    KeyTable["xstick1right"] = 126
    KeyTable["xstick2up"] = 127
    KeyTable["xstick2down"] = 128
    KeyTable["xstick2left"] = 129
    KeyTable["xstick2right"] = 130
    
    Times2["attack"] = 1
    Times2["jump"] = 2
    Times2["duck"] = 4
    Times2["forward"] = 8
    Times2["back"] = 16
    Times2["use"] = 32
    Times2["cancel"] = 64
    Times2["left"] = 128
    Times2["right"] = 256
    Times2["moveleft"] = 512
    Times2["moveright"] = 1024
    Times2["attack2"] = 2048
    Times2["run"] = 4096
    Times2["reload"] = 8192
    Times2["alt1"] = 16384
    Times2["alt2"] = 32768 
    Times2["score"] = 65536
    Times2["speed"] = 131072
    Times2["walk"] = 262144
    Times2["zoom"] = 524288
    Times2["weapon1"] = 1048576
    Times2["weapon2"] = 2097152
    Times2["bullrush"] = 4194304
    
    local function KeyChangeHook(ply, key)
    	if IsTriggeringForKey[ply] and IsTriggeringForKey[ply][key] then
    		for k,_ in pairs(IsTriggeringForKey[ply][key])do
    			if k and k:IsValid() then
    				k:Execute()
    			else
    				IsTriggeringForKey[ply][key][k] = nil
    			end
    		end
    	end
    end
    hook.Add( "KeyPress", "KeyPressedHook", KeyChangeHook)
    hook.Add( "KeyRelease", "KeyReleasedHook", KeyChangeHook)
    
    end
    
    registerFunction("keyDown", "s", "n", function(self,args)
        local op1 = args[2]
        local rv1 = op1[1](self,op1)
    	rv1 = string.lower(rv1)
    	if self.player:KeyDown(Times2[rv1]) then
    		return 1
    	end
    	return 0
    end)
    
    registerFunction("triggerForKey", "s", "", function(self,args)
        local op1 = args[2]
        local rv1 = op1[1](self,op1)
    	IsTriggeringForKey[self.player] = IsTriggeringForKey[self.player] or {}
    	rv1 = string.lower(rv1)
    	rv1 = string.Explode(",",rv1)
    	for _,v in pairs(rv1) do
    		v = Times2[v]
    		if v then
    			IsTriggeringForKey[self.player][v] = IsTriggeringForKey[self.player][v] or {}
    			if !IsTriggeringForKey[self.player][v][self.entity] then
    				IsTriggeringForKey[self.player][v][self.entity] = true
    			end
    		end
    	end
    end)
    
    registerFunction("triggerForKeyStop", "s", "", function(self,args)
        local op1 = args[2]
        local rv1 = op1[1](self,op1)
    	IsTriggeringForKey[self.player] = IsTriggeringForKey[self.player] or {}
    	rv1 = string.lower(rv1)
    	rv1 = string.Explode(",",rv1)
    	for _,v in pairs(rv1) do
    		v = Times2[v]
    		if v and IsTriggeringForKey[self.player][v] and IsTriggeringForKey[self.player][v][self.entity] then
    			IsTriggeringForKey[self.player][v][self.entity] = nil
    		end
    	end
    end)
    
    registerFunction("cListenForKey", "s", "", function(self,args)
        local op1 = args[2]
        local rv1 = op1[1](self,op1)
    	IsListeningForKey[self.player] = IsListeningForKey[self.player] or {}
    	rv1 = string.lower(rv1)
    	rv1 = string.Explode(",",rv1)
    	local started = false
    	for _,v in pairs(rv1) do
    		v = KeyTable[v]
    		if v and !IsListeningForKey[self.player][v] then
    			if !started then umsg.Start("e2keystartlistening", self.player) started=true end
    			umsg.Short(v)
    			IsListeningForKey[self.player][v] = 0
    		end
    	end
    	if started then umsg.End() end
    end)
    
    registerFunction("cListenForKeyStop", "s", "", function(self,args)
        local op1 = args[2]
        local rv1 = op1[1](self,op1)
    	IsListeningForKey[self.player] = IsListeningForKey[self.player] or {}
    	rv1 = string.lower(rv1)
    	rv1 = string.Explode(",",rv1)
    	local started = false
    	for _,v in pairs(rv1) do
    		v = KeyTable[v]
    		if v and IsListeningForKey[self.player][v] then
    			if !started then umsg.Start("e2keystartlistening", self.player) started=true end
    			umsg.Short(-v)
    			IsListeningForKey[self.player][v] = nil
    		end
    	end
    	if started then umsg.End() end
    end)
    
    registerFunction("cTriggerForKey", "s", "", function(self,args)
        local op1 = args[2]
        local rv1 = op1[1](self,op1)
    	cIsTriggeringForKey[self.player] = cIsTriggeringForKey[self.player] or {}
    	rv1 = string.lower(rv1)
    	rv1 = string.Explode(",",rv1)
    	for _,v in pairs(rv1) do
    		v = KeyTable[v]
    		if v then
    			cIsTriggeringForKey[self.player][v] = cIsTriggeringForKey[self.player][v] or {}
    			cIsTriggeringForKey[self.player][v][self.entity] = true
    		end
    	end
    end)
    
    registerFunction("cTriggerForKeyStop", "s", "", function(self,args)
        local op1 = args[2]
        local rv1 = op1[1](self,op1)
    	if !cIsTriggeringForKey[self.player] then return end
    	rv1 = string.lower(rv1)
    	rv1 = string.Explode(",",rv1)
    	for _,v in pairs(rv1) do
    		v = KeyTable[v]
    		if v and cIsTriggeringForKey[self.player][v] and cIsTriggeringForKey[self.player][v][self.entity] then
    			cIsTriggeringForKey[self.player][v][self.entity] = nil
    		end
    	end
    end)
    
    registerFunction("cKeyDown", "s", "n", function(self,args)
        local op1 = args[2]
        local rv1 = op1[1](self,op1)
    	rv1 = string.lower(rv1)
    	if !KeyTable[rv1] then return end
    	rv1 = KeyTable[rv1]
    	if IsListeningForKey[self.player] and IsListeningForKey[self.player][rv1] then
    		return IsListeningForKey[self.player][rv1]
    	end
    	return 0
    end)
    
    registerFunction("cIgnoreChat", "n", "", function(self,args)
        local op1 = args[2]
        local rv1 = op1[1](self,op1)
    	rv1 = rv1~=0
    	IsListeningForKey[self.player] = IsListeningForKey[self.player] or {}
    	if IsListeningForKey[self.player] and IsListeningForKey[self.player].chat!=rv1 then
    		if rv1 then rv1=132 else rv1=131 end
    		umsg.Start("e2keystartlistening", self.player)
    			umsg.Short(rv1)
    		umsg.End()
    		IsListeningForKey[self.player].chat = rv1
    	end
    end)
    I'd rather not re-write anything else, the wiki has enough info.
    Now stop asking for input support mk?

    I've attached a zip with the .lua if you're too lazy to make one yourself.
    Enjoy.
    Attached Files Attached Files
    Last edited by ZeikJT; 04-03-2009 at 07:51 PM.
    Against stupidity the Gods themselves contend in vain.
    -Friedrich Schiller

    The flame puts me in the mood to "Do it!".
    -Dart, Legend of Dragoon

  2. #2
    Wire Sofaking Jimlad's Avatar
    Join Date
    Dec 2008
    Posts
    941

    Default Re: E2 - Input Support

    Brilliant! Any plans to have this included in the official SVN? Or are you waiting for "public testing" to confirm there aren't any issues? (Obviously you'd want to take out the E:cKeyDown(N) stuff for that though)
    Last edited by Jimlad; 03-18-2009 at 08:08 AM.

  3. #3
    Wire Sofaking Echo51's Avatar
    Join Date
    Feb 2009
    Location
    Denmark
    Posts
    1,489

    Default Re: E2 - Input Support

    So we can see what our clients do using this, and it allows keyboard in E2?
    Proud of my non-existant life...
    In russia, Expression 2 codes you!
    Quote Originally Posted by Snowden42 View Post
    Nooblishious? That's a signature keeper.

  4. #4
    Wire Sofaking ZeikJT's Avatar
    Join Date
    Dec 2008
    Location
    California
    Posts
    1,391

    Default Re: E2 - Input Support

    You will only be able to "see" keys that have been registered by the user through E2 or by using the serverside key registers. Though they are limited I'm sure you could still get some usefulness out of it.
    This part about seeing other people's key was just a bit of fun on my part, it's easily removed as the two commands that do that are completely dependent on the other code without the other code depending on them.

    As per the SVNed bit, I honestly don't know and don't care. I just wanted to stop people from asking for it.
    Against stupidity the Gods themselves contend in vain.
    -Friedrich Schiller

    The flame puts me in the mood to "Do it!".
    -Dart, Legend of Dragoon

  5. #5
    Expressionism 2.0 Syranide's Avatar
    Join Date
    Mar 2007
    Location
    Sweden
    Posts
    4,573

    Default Re: E2 - Input Support

    Doesn't this catch what you type in the chat as well?

  6. #6
    Wire Amateur Pyroclastic's Avatar
    Join Date
    Jan 2009
    Posts
    47

    Default Re: E2 - Input Support

    ZeikJT is my god.
    I seriously might need new pants if this was SVNed.

  7. #7
    Wire Sofaking ZeikJT's Avatar
    Join Date
    Dec 2008
    Location
    California
    Posts
    1,391

    Default Re: E2 - Input Support

    Added the chat keystroke ignore command.
    Against stupidity the Gods themselves contend in vain.
    -Friedrich Schiller

    The flame puts me in the mood to "Do it!".
    -Dart, Legend of Dragoon

  8. #8
    Wire Amateur Pyroclastic's Avatar
    Join Date
    Jan 2009
    Posts
    47

    Default Re: E2 - Input Support

    It doesn't seem to work, all I get is:

    No such function: keyDown(number) at line 12, char 7entities/gmod_wire_expression2/init.lua:204: bad argument #1 to 'pairs' (table expected, got nil)

    Even on the wiki examples.

  9. #9
    Wire Sofaking ZeikJT's Avatar
    Join Date
    Dec 2008
    Location
    California
    Posts
    1,391

    Default Re: E2 - Input Support

    Quote Originally Posted by Pyroclastic View Post
    It doesn't seem to work, all I get is:

    No such function: keyDown(number) at line 12, char 7entities/gmod_wire_expression2/init.lua:204: bad argument #1 to 'pairs' (table expected, got nil)

    Even on the wiki examples.
    Alright, I talked to Pyroclastic and thankfully someone else had already pointed out the problem and the wiki has been updated to fix it.

    In case you were wondering, it was because the expression loads the extensions before the wire keyboard loads, but whenever I was testing it I always loaded it post initialization (developer concommands) so the keyboard had already configured the keymappings. I added a check so that if the mappings don't yet exist they will be added.
    Against stupidity the Gods themselves contend in vain.
    -Friedrich Schiller

    The flame puts me in the mood to "Do it!".
    -Dart, Legend of Dragoon

  10. #10
    Wire Amateur Pyroclastic's Avatar
    Join Date
    Jan 2009
    Posts
    47

    Default Re: E2 - Input Support

    Not to threadjack, but I'm having trouble with some helper functions I wrote for the input support.

    [highlight=lua]
    registerFunction("cKeyValue", "s", "n", function(self, args)
    local op1 = args[2]
    local rv1 = op1[1](self,op1)
    return string.byte(rv1)
    end)

    registerFunction("keyValue", "s", "n", function(self, args)
    local op1 = args[2]
    local rv1 = op1[1](self, op1)
    if !InvertedKeyMap[string.byte(rv1)] then return 0 end
    return InvertedKeyMap[string.byte(rv1)]
    end)
    [/highlight]

    Thats what I've got so far, I'm fairly certain I'm doing it wrong as its not giving me the right values. This probably is because of the discrepancy between serverside and clientside key values.

    keyDown(N) uses the IN_KEYS ones, correct?
    cKeyDown(N) uses ASCII then?

    What value would string.byte() return, IN_KEYS or ASCII?

    InvertedKeyMap is ASCII to IN_KEYS and Keyboard_ReMap is the opposite?

    I know its a lot of questions, but I'm considerably confused on the matter, and I think these functions would be helpful.

    Also, any possibility of this being SVNed? I'd love to be able to use it on other servers.

+ Reply to Thread
Page 1 of 6 123 ... LastLast

Similar Threads

  1. G15 support
    By Hunter234564 in forum Ideas & Suggestions
    Replies: 10
    Last Post: 03-22-2009, 08:54 AM
  2. Life Support
    By willthemage in forum Installation and Malfunctions Support
    Replies: 8
    Last Post: 08-20-2008, 02:06 PM
  3. Replies: 16
    Last Post: 02-26-2008, 07:29 AM
  4. Extended LS2 Support
    By WhiskeyFur in forum Ideas & Suggestions
    Replies: 5
    Last Post: 12-20-2007, 05:29 PM
  5. Packet Support for Everything
    By Razara in forum Ideas & Suggestions
    Replies: 3
    Last Post: 07-26-2007, 05:30 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
proceed-collector
proceed-collector
proceed-collector
proceed-collector
linguistic-parrots
linguistic-parrots
linguistic-parrots
linguistic-parrots