Get a key the player is using

How would I capture what button a player presses within a certain time frame?
Something like ‘Press the key you want to bind this to’ I guess.
I really have no way in mind to do it. If you could give me a direction on to what hook to use or something and how I would, that would be awesome, Thanks :slight_smile:

UserCMD:GetButtons() in http://wiki.garrysmod.com/page/GM/CreateMove

Thanks! When I try it it seems to only return the showscores key, am I doing something wrong?



local bindthing = false

local function tickthing( cmd )
  if bindthing then
    lastbutton = cmd:GetButtons()
  else
    lastbutton = nil
  end
end
hook.Add( "CreateMove", "playerbingthing", tickthing )

BAKSUSTAUNT.createBindThing = function( button, command )
  if IsValid( BAKSUSTAUNT.bind ) then BAKSUSTAUNT.bind:Close() end
  surface.SetFont( "BaksusTaunt32")
  local text = string.upper( "Press a key to bind the action." ) -- lazy
  local w, th = surface.GetTextSize( text )
  local h = th*2+30+50

  BAKSUSTAUNT.bind = vgui.Create( "DFrame" )
  BAKSUSTAUNT.bind:SetSize( w + 10, h )
  BAKSUSTAUNT.bind:Center()
  BAKSUSTAUNT.bind:ShowCloseButton( false )
  BAKSUSTAUNT.bind:SetDraggable( false )
  BAKSUSTAUNT.bind:MakePopup()
  BAKSUSTAUNT.bind:SetTitle( "" )
  BAKSUSTAUNT.bind.Paint = function( self, w, h )
    Derma_DrawBackgroundBlur( BAKSUSTAUNT.bind, .2 )
    draw.RoundedBox( 0, 0, 0, w, h, BAKSUSTAUNT.Colors.Alt1 )
    draw.SimpleText( text, "BaksusTaunt32", 5, 5, BAKSUSTAUNT.Colors.FontColor, TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP )
    draw.SimpleText( "<ENTER KEY>", "BaksusTaunt32", w/2, 15 + th, BAKSUSTAUNT.Colors.FontColor, TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP )
  end
  BAKSUSTAUNT.bind.Think = function( self )
    if lastbutton ~= nil then
      ply:ChatPrint( lastbutton )
      --self:Close()
      --bindthing = false
    end
  end

  local cancel = vgui.Create( "DButton", BAKSUSTAUNT.bind )
  cancel:SetSize( w-10, 50 )
  cancel:SetPos( 5, h-50-5 )
  cancel:SetText( "" )
  cancel.DoClick = function( self )
    BAKSUSTAUNT.bind:Close()
    bindthing = false
  end
  cancel.Paint = function( self, w, h )
    draw.RoundedBox( 0, 0, 0, w, h, BAKSUSTAUNT.Colors.Alt2 )
    draw.SimpleText( "CANCEL", "BaksusTaunt32", w/2, h/2, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
  end

  timer.Simple( 1 , function()
    bindthing = true
  end)
end


dont mind the messiness and stuff

You could use a DBinder

[editline]23rd June 2016[/editline]

It’s code is here, and it uses key trapping to find the key

If you want to copy the DBinder’s functionality without using it, you can use

input.StartKeyTrapping to start key trapping, then

input.CheckKeyTrapping to get the key

Woah I should probably read the docs a little more thoroughly… I never knew that existed, Thank you!

:snip: automerge

Yep! Works like a charm, thanks again