The Counting Gate Keeper

TI-Basic, 22 bytes

Fairly simple. Sets a time Ans seconds in the future which we call T which is when the loop stops. While the loop is going, we check for if a key is pressed. It's been a while since I had the fgitw :)

startTmr+Ans->T
0
While T≠startTmr
Ans+not(not(getKey
End

Ruby, 38 42 bytes

Uses the global variable $., which counts how many times the standard input function gets has successfully been completed. As such, it will only give accurate results the first time it is called per program run.

Input via command-line argument, such as ruby gatekeeper.rb 40

Thread.new{loop{gets}}
sleep eval$*[0]
p$.

AHK, 110 bytes

1*=-1000
c=0
Loop,255
Hotkey,% Format("vk{:x}",A_Index),s,On
SetTimer,q,%1%
Return
s:
c++
Return
q:
MsgBox,%c%

This is not as short as other submissions, but it distinguishes itself by allowing you to use practically any key as a counter. This includes mouse clicks and control keys like Ctrl, Shift, etc. unless you use special keys that can never be blocked such Ctrl+Alt+Del on Windows.

Two important notes:

  • The key presses and mouse clicks are blocked from other software
  • The program does not terminate

If you actually run this program, it will count for 1 seconds (where the variable 1 is the first passed parameter), all the while counting how many key presses you made and blocking each one. When that time expires, it pops up a message box with the total. However, seeing as how all keyboard and mouse inputs are still blocked, it's rather difficult to dismiss that message. That sounds like a job for the next gatekeeper.

Tags:

Date

Code Golf