[Win32utils-devel] Screen capture, save to file

Shashank Date sdate at everestkc.net
Tue Apr 26 23:36:12 EDT 2005


Hi Paul,

Shashank Date wrote:

> Is it because  '_' (underscore)  requires pressing of the Shift key
> without which it is simply '-' (hyphen)?

I guessed it right. This worked for me:

##   Screen Capture program By Park Heesob
##
require 'Win32API'

KEYEVENTF_KEYUP = 0x2
SW_HIDE         = 0
SW_SHOW         = 5
SW_SHOWNORMAL   = 1
VK_CONTROL      = 0x11
VK_F4           = 0x73
VK_MENU         = 0x12
VK_RETURN       = 0x0D
VK_SHIFT        = 0x10
VK_SNAPSHOT     = 0x2C
VK_TAB      = 0x09

def screenCapture(filename = "C:\\watir_bonus\\tmp.bmp")

  keybd_event = Win32API.new("user32", "keybd_event", ['I','I','L','L'], 
'V')
  vkKeyScan = Win32API.new("user32", "VkKeyScan", ['I'], 'I')
  winExec = Win32API.new("kernel32", "WinExec", ['P','L'], 'L')
 
  filename = Dir.getwd.tr('/','\\') + '\\' + filename unless 
filename.index('\\')

  keybd_event.Call(VK_SNAPSHOT,0,0,0)   # Print Screen
  # keybd_event.Call(VK_SNAPSHOT,1,0,0) # Alt+Print Screen

  winExec.Call('mspaint.exe', SW_SHOW)
  sleep(1)
 
  # Ctrl + V  : Paste
  keybd_event.Call(VK_CONTROL, 1, 0, 0)
  keybd_event.Call(vkKeyScan.Call(?V), 1, 0, 0)
  keybd_event.Call(vkKeyScan.Call(?V), 1, KEYEVENTF_KEYUP, 0)
  keybd_event.Call(VK_CONTROL, 1, KEYEVENTF_KEYUP, 0)

  # Alt E + O : Save Selected Image
  keybd_event.Call(VK_MENU, 1, 0, 0)
  keybd_event.Call(vkKeyScan.Call(?E), 1, 0, 0)
  keybd_event.Call(vkKeyScan.Call(?E), 1, KEYEVENTF_KEYUP, 0)
  keybd_event.Call(VK_MENU, 1, KEYEVENTF_KEYUP, 0)
  keybd_event.Call(vkKeyScan.Call(?O), 1, 0, 0)
  keybd_event.Call(vkKeyScan.Call(?O), 1, KEYEVENTF_KEYUP, 0)
  sleep(1)
 
  # Enter filename
  filename.each_byte { |ch|     
   keybd_event.Call(VK_SHIFT, 1, 0, 0) if [?:,?_,?$].include?(ch)
   keybd_event.Call(vkKeyScan.Call(ch), 1, 0, 0)
   keybd_event.Call(vkKeyScan.Call(ch), 1, KEYEVENTF_KEYUP, 0)
   keybd_event.Call(VK_SHIFT, 1, KEYEVENTF_KEYUP, 0) if 
[?:,?_,?$].include?(ch)
   sleep(1)
  }
  keybd_event.Call(VK_RETURN, 1, 0, 0)
  keybd_event.Call(VK_RETURN, 1, KEYEVENTF_KEYUP, 0)
  sleep(1)
 
  # Y
  keybd_event.Call(vkKeyScan.Call(?Y), 1, 0, 0)
  keybd_event.Call(vkKeyScan.Call(?Y), 1, KEYEVENTF_KEYUP, 0)
  sleep(1)
 
  # Alt + F4 : Exit
  keybd_event.Call(VK_MENU, 1, 0, 0)
  keybd_event.Call(VK_F4, 1, 0, 0)
  keybd_event.Call(VK_F4, 1, KEYEVENTF_KEYUP, 0)
  keybd_event.Call(VK_MENU, 1, KEYEVENTF_KEYUP, 0)
  sleep(3) 
 
  # N
  keybd_event.Call(vkKeyScan.Call(?N), 1, 0, 0)
  keybd_event.Call(vkKeyScan.Call(?N), 1, KEYEVENTF_KEYUP, 0)
 
end

screenCapture



More information about the win32utils-devel mailing list