Minecraft randomly pauses

Solution 1:

I believe this issue is with the hibernation time of your PC. If you have been AFKing for long periods of time, the PC will hibernate(I'm not very sure of the term) and Mine-craft will go to the pause menu. To fix this issue FOR PC go to Settings -> System -> Power and sleep, the length of time that your PC will hibernate at will be there and you can change it.

For Mac go to System Preferences -> Energy Saver

Solution 2:

I have had the reverse problem, most prominently in Skyrim, where my character would continue walking after I'd let go of the keys for up to four seconds.

I tried a few things to change this behaviour, but finally, conjecture lead me to believe it was the high temperature of the keyboard causing this behaviour: the only buttons affected were the ones close to the top-left of the keyboard, where my laptop (due to some illogical engineering decisions) gets the hottest (the fact that these buttons are groped most of the time doesn't help).

And, indeed, switching to an external keyboard solved it completely. (Of course, this is also a good first step in isolating the problem.)
I experienced improvements using a laptop cooler, as well.

I vaguely remember having that same problem as you as well, but can't for the life of me remember what game it was or how I solved it. It does remind me, however, that you also might want to check whether or not Windows' Sticky Keys is turned on.