Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing to Excel file
#1
I have written a simple program that captures keystroke data. I created two functions (on_Press and on_release), I want to write all captured keystrokes data from on_press() & on_release() to a separate excel file every time the enter key is pressed or code executed.

But after executing below code, the program is only writing the "Enter" key data and ignoring the rest.

Any ideas, please?

Thanks

from pynput.keyboard import Key, Listener
from datetime import datetime
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile



key_pressed = [0]
key_press_time = [0]
key_released = [0]
key_release_time = [0]


def on_press(key):
    global key_pressed, key_press_time, key_released, key_release_time
    key_pressed = key
    key_press_time = datetime.utcnow().strftime('%M:%S.%f')

    return key_pressed, key_press_time


def on_release(key):
    global key_pressed, key_press_time, key_released, key_release_time
    key_released = key
    key_release_time = datetime.utcnow().strftime('%M:%S.%f')

    key_dict = {'Key_Pressed':[key_pressed],
                'Key_Press_Time':[key_press_time],
                'Key_Released':[key_released],
                'Key_Release_Time':[key_release_time]}

    kd = pd.DataFrame(key_dict)

    print(kd)


    if key == Key.enter:
        writer = ExcelWriter('KeystrokeData.xlsx')
        kd.to_excel(writer,'Sheet1',index=False)
        writer.save()
        # Stop listener
        return False




# Collect events until released
with Listener(
        on_press=on_press,
        on_release=on_release) as listener:
    listener.join()
Reply
#2
The listener is capturing all of the keystrokes which is what you need if you want to capture 'Enter' key.
(I can see it happening when I run your code)

But these are interrupts, and all keys are going to be serviced immediately, no matter what you are doing,
possibly when writing to ExcelWriter (unless interrupts are disabled during the write).
I think this task is up to you, and you need to turn the listener off during this time.
Reply
#3
Thank you for the response. I will try turning the listener off and see if it works

Regards
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Automated filler for an Excel form is not writing the data Quian34 2 36 Jun-10-2026, 07:27 AM
Last Post: Larz60+
Question [SOLVED] Linefeed when writing "f" strings to text file? Winfried 5 858 Nov-04-2025, 11:51 AM
Last Post: buran
  .xlsm file can't be opened after writing data to one worksheet mhyatt 4 885 Oct-11-2025, 11:25 PM
Last Post: Pedroski55
  Problems writing a large text file in python Vilius 4 2,033 Dec-21-2024, 09:20 AM
Last Post: Pedroski55
  writing list to csv file problem jacksfrustration 5 4,139 Jul-04-2024, 08:15 PM
Last Post: deanhystad
  docx file to pandas dataframe/excel iitip92 1 5,689 Jun-27-2024, 05:28 AM
Last Post: Pedroski55
  Python openyxl not updating Excel file MrBean12 1 3,950 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 2,383 Feb-07-2024, 12:24 PM
Last Post: Viento
  Search Excel File with a list of values huzzug 4 5,059 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 2,968 Nov-03-2023, 05:13 PM
Last Post: noisefloor

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020