simulate a key press


Maybe my question is very simple but I have no idea how to do it....
here´s my problem...

I have to take a screenshot of my computer every day at the same time and upload it to a server...
I was thinking to use a scheduled task in Windows to do this...after a long search for the right capturing program, Screenshot Utility 1.0 was the best I could find.
In this program you can configure your capture key and then de screenshot wil be saved at a pre-defined place.
Now for my question, I need to make a program wich simulates a press on the keyboard(for example C)...
I have no idea how to do that...The languages i know are powerbuilder, java en PHP...
but maybe I´ll have to use VB.

How can I do it the best and easiest way?!?!

thanks in advance,

Who is Participating?
you could try the keybd event api:

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Const KEYEVENTF_KEYUP = &H2
Private Const VK_C = &H43

' press C key
keybd_event VK_C, 0, 0, 0

' release C key
keybd_event VK_C, 0, KEYEVENTF_KEYUP, 0

If you are using vbscript you can always use the SendKeys method.  More about it here:
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Sub Command1_Click()
    keybd_event vbKeySnapshot, 0, 0, 0
    SavePicture Clipboard.GetData, "c:\my.jpg"
End Sub

~ fantasy ~
to simulate a keypres, all u need to do is use the SendKeys method in VB.

To capture the desktop,


Option Explicit
Private Declare Function GetWindowRect Lib "USER32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function GetDesktopWindow Lib "USER32" () As Long

Private Declare Function GetActiveWindow Lib "USER32" () As Long
Private Declare Function GetTopWindow Lib "USER32" (ByVal hwnd As Long) As Long
Private Declare Function GetClientRect Lib "USER32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function GetWindowDC Lib "USER32" (ByVal hwnd As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function ReleaseDC Lib "USER32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type
Private Type BITMAP
        bmType As Long
        bmWidth As Long
        bmHeight As Long
        bmWidthBytes As Long
        bmPlanes As Integer
        bmBitsPixel As Integer
        bmBits As Long
End Type

Public Sub GetShot()
'(lWindowhWnd As Long)
    Dim lWindowhWnd As Long
    Dim nLeft As Long
    Dim nTop As Long
    Dim nWidth As Long
    Dim nHeight As Long
    Dim rRect As RECT
    Dim bm As BITMAP
    Dim lWindowhDC As Long
    lWindowhWnd = GetDesktopWindow
    Set Form1.picScreen.Picture = Nothing
    GetWindowRect lWindowhWnd, rRect
    lWindowhDC = GetWindowDC(lWindowhWnd)
    '// Get coordinates
    nLeft = 0
    nTop = 0
    nWidth = rRect.Right - rRect.Left
    nHeight = rRect.Bottom - rRect.Top
    '// Blt to frm.picScreen
    BitBlt Form1.picScreen.hdc, 0, 0, nWidth, nHeight, lWindowhDC, nLeft, nTop, vbSrcCopy
    '// Del DC
    ReleaseDC lWindowhWnd, lWindowhDC
    '// set picture
    Form1.picScreen.Picture = Form1.picScreen.image
End Sub

this bit of code, gets the screenshot into a picturebox placed in form1.

u cud use the savepicture function to save the pic to a bmp and do the stuff u want.

If u want a jpeg, i'd suggest u use the intel jpeg compressor library etc.

I think you can make it even simpler and handle it all on your own.

Create a form and place a picturebox.

Thanks to Vinnyd79 sub, do the following:

keybd_event VK_SNAPSHOT, 0, 0, 0

then, take the clipboard data and place it in the picture (for easy handling image)
Picture1.Picture = Clipboard.GetData

If you need just the active window, use the following (of course your ap should run not active)

keybd_event VK_SNAPSHOT, 1, 0, 0

At this point, you could easily save you Bitmap wherever you need.

To save it, look for "Saving a Window's Client Area in Bitmap Format" in MSDN (on the web) for a BMP save or look for your format specific functions.



Forgot to say, you cannot send PrintScreen with SendKeys, it will not work!!! Use the Vinnyd79 (keybd_event) function only!!!

And take care of sending the keyup event or nothing will be copied to the clipdoard.


Listening . . .
Moderator, my recommended disposition is:

    Split points between: vinnyd79 and MaxPol

Dan Rollins -- EE database cleanup volunteer
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.