simulate a key press

Posted on 2003-11-11
Last Modified: 2008-02-01

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,

Question by:wbph
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 28

Expert Comment

ID: 9726697
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

LVL 28

Accepted Solution

vinnyd79 earned 100 total points
ID: 9726703

Expert Comment

ID: 9726717
If you are using vbscript you can always use the SendKeys method.  More about it here:
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.


Expert Comment

ID: 9727041
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 ~

Expert Comment

ID: 9728405
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.

Expert Comment

ID: 9730599

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.




Assisted Solution

MaxPol earned 100 total points
ID: 9730619
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.



Expert Comment

ID: 9734700
Listening . . .
LVL 49

Expert Comment

ID: 10537259
Moderator, my recommended disposition is:

    Split points between: vinnyd79 and MaxPol

Dan Rollins -- EE database cleanup volunteer

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question