Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Writing to Excel file
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?


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],

    kd = pd.DataFrame(key_dict)


    if key == Key.enter:
        writer = ExcelWriter('KeystrokeData.xlsx')
        # Stop listener
        return False

# Collect events until released
with Listener(
        on_release=on_release) as listener:
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.
Thank you for the response. I will try turning the listener off and see if it works


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Avoiding empty line in writing process csv file go127a 4 133 May-10-2019, 01:33 PM
Last Post: go127a
  csv to xlsx, remove " while writing to excel anna 3 151 Apr-16-2019, 07:57 AM
Last Post: anna
  writing in particular positions clarablanes 5 203 Apr-13-2019, 04:07 PM
Last Post: clarablanes
  Openpyxl - while saving excel file getting error shubhamjainj 1 131 Apr-09-2019, 12:05 PM
Last Post: Larz60+
  Trouble writing over serial EngineerNeil 1 118 Apr-07-2019, 08:17 PM
Last Post: j.crater
  Putting text on images from excel/csv file sam77am 1 108 Apr-05-2019, 07:36 AM
Last Post: Larz60+
  Writing integer to file jpezz 4 239 Apr-02-2019, 07:22 PM
Last Post: jpezz
  Who converts data when writing to a database with an encoding different from utf8? AlekseyPython 1 152 Mar-04-2019, 08:26 AM
Last Post: DeaD_EyE
  Getting error while loading excel(.xlsx) file using openpyxl module shubhamjainj 1 269 Mar-01-2019, 01:05 PM
Last Post: buran
  Writing commands to serial python_beginner 7 14,987 Feb-03-2019, 09:03 AM
Last Post: jambuna35

Forum Jump:

Users browsing this thread: 1 Guest(s)