Key Strokes

I’m new to scripting, and I wanted to make a script that would record every key that was pressed for say 100 keystrokes. Is this even possible, if so, how might I go about doing it. I know I’m new to this subject, and this might be a little advanced but if anyone has any ideas, I would love them. Thanks!!

What problem are you trying to solve? I’m sure there’s a better way.

Unless you’re writing spyware. Cough, cough.

I just want to make a script for my mac that would record keystrokes, or whatever keys are pressed

You could capture keystrokes using a third-party application, or a scripting addition such as Jon’s Commands. (While I’d normally suggest Extra Suites as a possible alternative, it currently seems to be harbouring a small bug that makes it less suitable for this particular purpose. I’ll contact the developer about this.)

I’ve suggested a possible script below, although a few points may be worth noting:

To test the routine, the following actions were taken:

To give you some idea of how keystrokes are represented, these are the results from the above actions:

Here’s the script:

property modifier_keys : {"Command", "Shift", "Caps Lock", "Option", "Control", "key64"}
property previous_keys : {}

on valid_keys(k)
	tell k to considering case
		if it is {".", "Command"} then error number -128
		if it is previous_keys then return false
		set previous_keys to it
		(count) > 0 and beginning is not in modifier_keys
	end considering
end valid_keys

to record_keys for n
	set {previous_keys, key_list} to {{}, {}}
	try (* return key_list if script execution is cancelled *)
		repeat while (count key_list) < n
			tell («event JonsgKey») to if my valid_keys(it) then set key_list's end to it
		end repeat
		beep (* signal completion of recording *)
	on error e number n
		if n is -1708 then error "This script requires Jon's Commands.osax."
		if n is not -128 then error e
	end try
	key_list
end record_keys

tell application "System Events" to set visible of first process where it is frontmost to false
record_keys for 100 (* maximum number of keystrokes to record *)

I still posit that, assuming he’s not trying to build a silent keylogger for nefarious purposes, that there’s gotta be a better solution to whatever it is that he’s trying to solve with this.

So, poster, what’s the haps?

I just thought it might be kind of cool to see if anyone used my comp when i was away, and i’m new to scripting so i don’t have very good ideas yet, or how to make anything. I was just interested in the “complex” codes might look