• Calling function when you press a KEY_
    7 replies, posted
So Im attempting to run a function when I press q, Ive tried all of these variations [CODE]local function KeyPress() if input.IsKeyDown(KEY_Q) then print("Test") end end hook.Add("Think","keypress",KeyPress) ALSO hook.Add("KeyPress","keypress",function( ply, key) if ( key == KEY_Q ) then print("test") end end) ALSO hook.Add("KEY_Q","keypress",function() print("Test") end) [/CODE]' All dont work, what do I do?
[QUOTE=Yikegaming;51202444] [code]hook.Add("KEY_Q","keypress",function() print("Test") end) [/CODE][/QUOTE] Ilold. This is not how it works. [code]hook.Add("KeyPress","keypress",function( ply, key) if ( key == KEY_Q ) then print("test") end end) [/code] Well, KeyPress passes [I]IN_ Enums[/I] as [I]key[/I] parameter while you're attempting to compare [I]key[/I] with [I]KEY_ Enums[/I]. With KeyPress you only can use [I]IN_ Enums[/I] --> [url]https://wiki.garrysmod.com/page/Enums/IN[/url] For proper use of [I]KEY_ Enums[/I] refer to [url]https://wiki.garrysmod.com/page/input/IsKeyDown[/url] i.e. this should work fine [CODE] hook.Add("KeyPress","keypress",function( ply, key) if ( input.IsKeyDown( KEY_Q ) ) then print("test") end end)[/CODE] Don't forget to make an unique name for any hooks. Cheers.
[QUOTE=igodsewer;51202459]Ilold. This is not how it works. [code]hook.Add("KeyPress","keypress",function( ply, key) if ( key == KEY_Q ) then print("test") end end) [/code] Well, KeyPress passes [I]IN_ Enums[/I] as [I]key[/I] parameter while you're attempting to compare [I]key[/I] with [I]KEY_ Enums[/I]. With KeyPress you only can use [I]IN_ Enums[/I] --> [url]https://wiki.garrysmod.com/page/Enums/IN[/url] For proper use of [I]KEY_ Enums[/I] refer to [url]https://wiki.garrysmod.com/page/input/IsKeyDown[/url] i.e. this should work fine [CODE] hook.Add("KeyPress","keypress",function( ply, key) if ( input.IsKeyDown( KEY_Q ) ) then print("test") end end)[/CODE] Don't forget to make an unique name for any hooks. Cheers.[/QUOTE] I get this error [CODE] attempt to index global 'input' (a nil value)[/CODE]
[QUOTE=igodsewer;51202459] [CODE] hook.Add("KeyPress","keypress",function( ply, key) if ( input.IsKeyDown( KEY_Q ) ) then print("test") end end)[/CODE][/QUOTE] Did not notice the hook name lol. It should be Think and function should be without any arguments. like this: [CODE] hook.Add("Think","keypress",function() if ( input.IsKeyDown( KEY_Q ) ) then print("test") end end) [/CODE] ps this would spam your console with "test" messages as you hold Q button.
[QUOTE=Yikegaming;51202477]I get this error [CODE] attempt to index global 'input' (a nil value)[/CODE][/QUOTE] input is clientside only
[QUOTE=JasonMan34;51202544]input is clientside only[/QUOTE] Is there a way to do this serverside then?
[QUOTE=Yikegaming;51203611]Is there a way to do this serverside then?[/QUOTE] You can always network it, but since you're using Q, why not just hook it into the[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/OnSpawnMenuOpen]GM:OnSpawnMenuOpen[/url] hook?
[QUOTE=kpjVideo;51205268]You can always network it, but since you're using Q, why not just hook it into the[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/OnSpawnMenuOpen]GM:OnSpawnMenuOpen[/url] hook?[/QUOTE] Oh... Didnt even know this existed, thanks!
Sorry, you need to Log In to post a reply to this thread.