Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 976
  • Last Modified:

simulate a key press

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
wbph
Asked:
wbph
2 Solutions
 
vinnyd79Commented:
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
 
tim_mcgueCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
fantasy1001Commented:
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
 
_agj_Commented:
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
 
MaxPolCommented:
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
 
MaxPolCommented:
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
 
Dang123Commented:
Listening . . .
0
 
DanRollinsCommented:
Moderator, my recommended disposition is:

    Split points between: vinnyd79 and MaxPol

Dan Rollins -- EE database cleanup volunteer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now