{ Science  Technology  Engineering  Mathematics }     |     Phnom Penh, Cambodia    |    Fantastic service at a fair price
STEM Phnom Penh
  • Home
  • Services
    • Tuition >
      • Courses
    • Training
    • Prototypes and Customs
    • Educational Robotics >
      • MB2 CCE robot >
        • Example Code - Javascript
        • Example Code - Python
        • MB2 CCE Calibration
  • Contact
  • Friends
  • Pages
  • SWM app embed test

Your computer is haunted! Ghost-typing in python

27/5/2015

Comments

 
Picture
Use this simple python program to haunt a computer. It runs silently in the background, and sends keyboard events to whatever window is in focus. You can copy a ghost story to a text file and set the program to start typing it out at any time.
Read this article in Khmer - អានអត្ថបទនេះជាភាសាខ្មែរ

What’s it good for?

Confusing/scaring people, and automating keyboard sequences that you frequently use. It only works on Windows computers that have Python 2 installed. You may also need to install thepywin32 module.
ghosttyping.pyw
File Size: 4 kb
File Type: pyw
Download File

ghost.txt
File Size: 0 kb
File Type: txt
Download File

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
# Small python script to ghost type on any focused window in windows.
# Based upon code by Michel Claveau: 
# https://mail.python.org/pipermail/python-win32/2013-July/012862.html

import win32api,time,win32con,random

def typethis(sentence=None,shift=False,control=False,delay=0.05,random_delay=0.05):
    for letter in sentence:
        time.sleep(random.random()*random_delay) # Makes it look like real typing
        c=letter
        punctflag = True
        if (letter>='A' and letter<='Z') or shift:
            win32api.keybd_event(win32con.VK_SHIFT, 0, 0, 0)
            punctflag = False
        if letter>='a' and letter<='z':
            c=letter.upper()
            punctflag = False
        if ((letter>='0' and letter<='9') or (letter==' ')):
            punctflag=False
        if control:
            win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0)

        if not punctflag: # Do this for real letters on the keyboard
            if isinstance(letter,(int)):
                ordletter=letter
            else:
                ordletter=ord(c)

            win32api.keybd_event(ordletter, 0, win32con.KEYEVENTF_EXTENDEDKEY | 0, 0) 
            time.sleep(delay)
            win32api.keybd_event(ordletter, 0, win32con.KEYEVENTF_EXTENDEDKEY | 
                        win32con.KEYEVENTF_KEYUP, 0)
            time.sleep(delay)

            if control:
                win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0)
            if (letter>='A' and letter<='Z') or shift:
                win32api.keybd_event(win32con.VK_SHIFT, 0, win32con.KEYEVENTF_KEYUP, 0)
        # This must be some punctuation - try to deal with it. 
        # win32con doesn't include OEM
        else: 
            if letter=='.':
                win32api.keybd_event(190, 0, win32con.KEYEVENTF_EXTENDEDKEY | 0, 0)
                time.sleep(delay)
                win32api.keybd_event(190, 0, win32con.KEYEVENTF_EXTENDEDKEY | 
                win32con.KEYEVENTF_KEYUP, 0)
                time.sleep(delay)
            elif letter==',':
                win32api.keybd_event(188, 0, win32con.KEYEVENTF_EXTENDEDKEY | 0, 0)
                time.sleep(delay)
                win32api.keybd_event(188, 0, win32con.KEYEVENTF_EXTENDEDKEY | 
                win32con.KEYEVENTF_KEYUP, 0)
                time.sleep(delay)
            elif letter=='-':
                win32api.keybd_event(189, 0, win32con.KEYEVENTF_EXTENDEDKEY | 0, 0)
                time.sleep(delay)
                win32api.keybd_event(189, 0, win32con.KEYEVENTF_EXTENDEDKEY | 
                win32con.KEYEVENTF_KEYUP, 0)
                time.sleep(delay)
            elif letter=='?':
                win32api.keybd_event(win32con.VK_SHIFT, 0, 0, 0)
                time.sleep(delay)
                win32api.keybd_event(191, 0, win32con.KEYEVENTF_EXTENDEDKEY | 0, 0)
                time.sleep(delay)
                win32api.keybd_event(191, 0, win32con.KEYEVENTF_EXTENDEDKEY | 
                win32con.KEYEVENTF_KEYUP, 0)
                time.sleep(delay)
                win32api.keybd_event(win32con.VK_SHIFT, 0, win32con.KEYEVENTF_KEYUP, 0)
                time.sleep(delay)
            elif letter=='!':
                win32api.keybd_event(win32con.VK_SHIFT, 0, 0, 0)
                time.sleep(delay)
                win32api.keybd_event(ord('1'), 0, win32con.KEYEVENTF_EXTENDEDKEY | 0, 0)
                time.sleep(delay)
                win32api.keybd_event(ord('1'), 0, win32con.KEYEVENTF_EXTENDEDKEY | 
                win32con.KEYEVENTF_KEYUP, 0)
                time.sleep(delay)
                win32api.keybd_event(win32con.VK_SHIFT, 0, win32con.KEYEVENTF_KEYUP, 0)
                time.sleep(delay)

    win32api.keybd_event(13, 0, win32con.KEYEVENTF_EXTENDEDKEY | 0, 0) 
    # There is a carriage return at the end of each newline
    
    if delay>0.0:
        time.sleep(delay)
        win32api.keybd_event(13, 0, win32con.KEYEVENTF_EXTENDEDKEY | 
        win32con.KEYEVENTF_KEYUP, 0)

def typefromfile(filename):
    with open(filename, 'r') as fopen: # with automatically closes the file
        for line in fopen:
            typethis(line)

time.sleep(3)  # switch focus to a target window manually in this time.

# Edit below this point to change what it types. 
# Supports lowercase and uppercase letters,
# numbers, and . , - ! ? (depending on your keyboard layout)

typethis("What is happening?")

typefromfile("C:/Users/User/Desktop/ghost.txt")

#typethis("all this should be in capslock", shift=True)

How to use it

Download ghosttyping.pyw (and ghost.txt) and open it in an editor. Change lines 94, 100 and 102 to customise the action of the file. You can make it pause, type in individual lines, or read from files, in any order, any number of times. Files with the .pyw extension should run with no visible window when you double click them. However, if you need to debug your code then resave the file as .py - this will run the code in command line and show you information about errors that occur.

Tips and tricks to improve it

Look up other Virtual Keyboard codes to extend the program. Knowing the Start Menu code means that you can startup a program like a browser or word-processor automatically before you type in any more text. Use typethis(“b”,control=True) to set the font to bold in the text. Use time.sleep(300) to wait 5 minutes before doing anything. Use typethis(“t”,control=True) to open a new browser tab, then enter an address - typethis automatically presses ‘enter’ after every time, which will set the page in the browser.

Discover more at colab/hackerspace for free. Just tell us what you’re interested in and when, and we’ll try to accommodate you.

Get personal tuition to learn python/programming, and other related subjects from STEM Phnom Penh. Fantastic tuition - fair price.


Comments
    All posts in English
    អត្ថបទទាំងអស់ជាភាសាខ្មែរ
    New entries coming soon!

    Archives

    June 2015
    May 2015

    Categories

    All
    123d
    3d
    3d Printing
    Bearings
    Blender
    Camera
    English
    Ghost
    Gimbal
    Google
    Guest Post
    Keyboard
    Keywords
    Khmer
    Programming
    Project
    Python
    Script
    SEO
    Sketchup
    Super Cheap
    Video
    Website

    RSS Feed

Copyright © 2015-2017 Chea Jofeh Co., Ltd. - all rights reserved

Contact info at stemphnompenh dot com for more information