| 
									
										
										
										
											2010-05-24 22:19:32 -04:00
										 |  |  | ! Copyright (C) 2010 Doug Coleman. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2010-06-10 17:39:13 -04:00
										 |  |  | USING: accessors timers bit-arrays calendar game.input io | 
					
						
							| 
									
										
										
										
											2010-05-24 22:19:32 -04:00
										 |  |  | io.binary io.encodings.binary io.files kernel literals math | 
					
						
							|  |  |  | namespaces system threads ;
 | 
					
						
							|  |  |  | IN: key-logger | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CONSTANT: frequency $[ 1/30 seconds ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CONSTANT: path "resource:key-log.txt" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : update-key-caps-state ( -- )
 | 
					
						
							|  |  |  |     read-keyboard keys>> | 
					
						
							|  |  |  |     path binary [ | 
					
						
							|  |  |  |         [ gmt unix-1970 time- duration>nanoseconds >integer ] | 
					
						
							|  |  |  |         [ bit-array>integer ] bi*
 | 
					
						
							|  |  |  |         [ 8 >be write ] bi@ flush
 | 
					
						
							|  |  |  |     ] with-file-appender ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SYMBOL: key-logger | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : start-key-logger ( -- )
 | 
					
						
							|  |  |  |     key-logger get-global [ | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             open-game-input | 
					
						
							|  |  |  |             [ update-key-caps-state ] frequency every key-logger set-global
 | 
					
						
							|  |  |  |         ] in-thread | 
					
						
							|  |  |  |     ] unless ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : stop-key-logger ( -- )
 | 
					
						
							| 
									
										
										
										
											2010-06-10 17:39:13 -04:00
										 |  |  |     key-logger get-global [ stop-timer ] when*
 | 
					
						
							| 
									
										
										
										
											2010-05-24 22:19:32 -04:00
										 |  |  |     f key-logger set-global
 | 
					
						
							|  |  |  |     close-game-input ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MAIN: start-key-logger |