keypress hook problem

hello everyone
i want to make a key hook , there is my code

hook.Add( “KeyPress”, “keypress_use_hi”, function( ply, key )
print(key)
end )

the problem is when i press a key, it sometimes print twice,but i need this only run once, is there any way to slove it ? thank you very much

I’m not sure if this is because of prediction, but you can try doing a IsFirstTimePredicted check in there.

Or you could just check if the key is different to the previous one:



local oldkey

hook.Add( "KeyPress", "keypress_use_hi", function( ply, key )
    if key ~= oldkey then
        print( key )
    end
    oldkey = key
end )


thank you very much , it fix my problem!!!

[editline]12th August 2016[/editline]

this way will cause another problem, when i press a key it work,but i press the same key again, it won’t work. use IsFirstTimePredicted can fix that, thank you any way