Solved

simulate a key press

Posted on 2003-11-11
11
948 Views
Last Modified: 2008-02-01
Hi!

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,

willem
0
Comment
Question by:wbph
11 Comments
 
LVL 28

Expert Comment

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

0
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 100 total points
ID: 9726703
0
 
LVL 3

Expert Comment

by:tim_mcgue
ID: 9726717
If you are using vbscript you can always use the SendKeys method.  More about it here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wsmthsendkeys.asp
0
 
LVL 5

Expert Comment

by:fantasy1001
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 ~
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 7

Expert Comment

by:_agj_
ID: 9728405
to simulate a keypres, all u need to do is use the SendKeys method in VB.

To capture the desktop,

use:

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
    'Hide
    Form1.picScreen.Cls
    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
    'Show
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.
0
 
LVL 2

Expert Comment

by:MaxPol
ID: 9730599
Hi,

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
keybd_event VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 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
keybd_event VK_SNAPSHOT, 1, KEYEVENTF_KEYUP, 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.

Bye,

Max

0
 
LVL 2

Assisted Solution

by:MaxPol
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.

Bye,

Max
0
 
LVL 9

Expert Comment

by:Dang123
ID: 9734700
Listening . . .
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 10537259
Moderator, my recommended disposition is:

    Split points between: vinnyd79 and MaxPol

Dan Rollins -- EE database cleanup volunteer
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now