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
  Python openyxl not updating Excel file MrBean12 1 250 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 348 Feb-07-2024, 12:24 PM
Last Post: Viento
  Search Excel File with a list of values huzzug 4 1,147 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 756 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,048 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 2,840 Feb-20-2023, 07:19 PM
Last Post: avd88
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 773 Feb-16-2023, 08:11 PM
Last Post: cubangt
  Import XML file directly into Excel spreadsheet demdej 0 801 Jan-24-2023, 02:48 PM
Last Post: demdej
  how to read txt file, and write into excel with multiply sheet jacklee26 14 9,525 Jan-21-2023, 06:57 AM
Last Post: jacklee26
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,071 Dec-15-2022, 04:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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