Atalanttore

KeyboardDetectionLogger

Nov 26th, 2020
640
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys
  2. from datetime import datetime
  3. import pythoncom
  4.  
  5. import wmi
  6.  
  7. from PyQt5. QtCore import QObject, QRunnable, QThreadPool, pyqtSignal
  8. from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem, QHeaderView
  9.  
  10.  
  11. class KeyboardDetectorSignals(QObject):
  12.     keyboard_changed = pyqtSignal(str)
  13.  
  14.  
  15. class KeyboardDetector(QRunnable):
  16.  
  17.     def __init__(self):
  18.         super().__init__()
  19.  
  20.         self.signals = KeyboardDetectorSignals()
  21.  
  22.     def run(self):
  23.         pythoncom.CoInitialize()
  24.         device_connected_wql = "SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA \'Win32_Keyboard\'"
  25.         device_disconnected_wql = "SELECT * FROM __InstanceDeletionEvent WITHIN 2 WHERE TargetInstance ISA \'Win32_Keyboard\'"
  26.  
  27.         c = wmi.WMI()
  28.         connected_watcher = c.watch_for(raw_wql=device_connected_wql)
  29.         disconnected_watcher = c.watch_for(raw_wql=device_disconnected_wql)
  30.  
  31.         while True:
  32.             try:
  33.                 connected = connected_watcher(timeout_ms=10)
  34.             except wmi.x_wmi_timed_out:
  35.                 pass
  36.             else:
  37.                 if connected:
  38.                     self.signals.keyboard_changed.emit("Keyboard connected.")
  39.  
  40.             try:
  41.                 disconnected = disconnected_watcher(timeout_ms=10)
  42.             except wmi.x_wmi_timed_out:
  43.                 pass
  44.             else:
  45.                 if disconnected:
  46.                     self.signals.keyboard_changed.emit("Keyboard disconnected.")
  47.  
  48.  
  49. class MainWindow(QMainWindow):
  50.  
  51.     def __init__(self):
  52.         super().__init__()
  53.  
  54.         self.setGeometry(100, 100, 500, 500)
  55.         self.setWindowTitle("Keyboard Logger")
  56.  
  57.         self.log_table = QTableWidget()
  58.         self.log_table.setColumnCount(2)
  59.         self.log_table.setShowGrid(True)
  60.         self.log_table.setHorizontalHeaderLabels(["Time", "Event"])
  61.         self.log_table.horizontalHeader().setStretchLastSection(True)
  62.         self.log_table.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
  63.         self.setCentralWidget(self.log_table)
  64.         self.show()
  65.  
  66.         self.threadpool = QThreadPool()
  67.         keyboard_detector = KeyboardDetector()
  68.         keyboard_detector.signals.keyboard_changed.connect(self.add_row)
  69.         self.threadpool.start(keyboard_detector)
  70.  
  71.     def add_row(self, event: str):
  72.         now = datetime.now()
  73.         datetime_string = now.strftime("%Y-%m-%d %H:%M:%S")
  74.  
  75.         row_count = self.log_table.rowCount()
  76.         self.log_table.insertRow(row_count)
  77.         self.log_table.setItem(row_count, 0, QTableWidgetItem(datetime_string))
  78.         self.log_table.setItem(row_count, 1, QTableWidgetItem(event))
  79.  
  80.     def closeEvent(self):
  81.         sys.exit(app.exec())
  82.  
  83.  
  84. def main():
  85.     app = QApplication(sys.argv)
  86.     window = MainWindow()
  87.     app.exec()
  88.  
  89.  
  90. if __name__ == '__main__':
  91.     main()
  92.  
RAW Paste Data