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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

839 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