include win32lib.ew
without warning

constant
	mystyle = {WS_CAPTION, WS_SYSMENU, WS_THICKFRAME},
	Win1 = create( Window, "Try It !", 0, 0, 0, 430, 85, mystyle ),
	Sample =create( Menu, "Sample", Win1, 0, 0, 0, 0, 0 ),
	About = create( MenuItem, "About", Sample, 0, 0, 0, 0, 0 ),
	Leave = create( MenuItem, "E&xit", Sample, 0, 0, 0, 0, 0 ),
	Button1 = create( PushButton, "ExplorerNew", Win1, 5, 5, 85, 25, 0 ),
	Button2 = create( PushButton, "FindFiles", Win1, 91, 5, 80, 25, 0 ),
	Button3 = create( PushButton, "MinimizeAll", Win1, 172, 5, 80, 25, 0 ),
	Button4 = create( PushButton, "RunDialog", Win1, 253, 5, 80, 25, 0 ),
	Button5 = create( PushButton, "StartMenu", Win1, 334, 5, 80, 25, 0 ),
	
	zVkKeyScan = registerw32Function( user32, "VkKeyScanA", {C_INT}, C_INT ),
	zkeybd_event = registerw32Function( user32, "keybd_event", {C_INT, C_INT, C_INT, C_INT}, C_INT ),
	zSleep = registerw32Procedure( kernel32, "Sleep", {C_INT} ),


KEYEVENTF_KEYUP = 2,
ExplorerNew = #45,   -- WIN key "E"
FindFiles   = #46,   -- ' ' key "F"
MinimizeAll = #4D,   -- ' ' key "M"
RunDialog   = #52,   -- ' ' key "R"
StartMenu   = #5B    -- ' ' key "["

atom jk
object ex,ex2,ex3

-- this gives outside dialogs a time slice,
-- so they don't miss key_fakes.
-- this *was* necessary for run_dialog()
procedure delay()
  w32Proc(zSleep,{25})
end procedure

procedure shift_out( atom shifted, atom key )
  jk = w32Func(zkeybd_event, {shifted, 0, 0, 0 } )
  jk = w32Func(zkeybd_event, {key, 0, 0, 0 } )
  delay()
  jk = w32Func(zkeybd_event, {shifted, 0, KEYEVENTF_KEYUP, 0 } )
end procedure

procedure key_out( atom key )
  jk = w32Func(zkeybd_event, {key, 0, 0, 0 } )
  delay()
end procedure


procedure explorer_new( integer self, integer event, sequence params )
   shift_out(VK_LWIN,ExplorerNew)
end procedure
setHandler( Button1, w32HClick, routine_id("explorer_new") )


procedure find_files( integer self, integer event, sequence params )
  ex2={}
  ex="WinKeys.exw" -- will handle '\r'...

  for i=1 to length(ex) do
      ex2=ex2 & w32Func(zVkKeyScan,{ex[i]})
  end for
  ex2=and_bits(#FF,ex2)  -- low byte
  shift_out(VK_LWIN,FindFiles)
  sleep(1)
  for i = 1 to length(ex2) do
       key_out(ex2[i])
  end for
  sleep(1)
  key_out(VK_RETURN) -- ... but, I'll do it here.
end procedure
setHandler( Button2, w32HClick, routine_id("find_files") )


procedure minimize_all( integer self, integer event, sequence params )
    shift_out(VK_LWIN,MinimizeAll)
end procedure
setHandler( Button3, w32HClick, routine_id("minimize_all") )

-- note: the next routine handles shifted keys...
-- like ":" and "_", the find_dialog() routine doesn't.
procedure run_dialog( integer self, integer event, sequence params )
integer word
  ex2={}
  ex3={}

  -- I'm assuming the following file exists

  ex="c:\\windows\\notepad.exe faq.txt"
  --ex="c:\\windows\\notepad.exe c:\\euphoria\\win2lib\\EuWinTutor2\\WinKeys.exw"

  --ex="\"c:\\ab cd\\12345 67890abc.exw\""
  --ex="\"c:\\ab cd\\longversion 999 for judith!.exw\""

  for i=1 to length(ex) do
      word=w32Func(zVkKeyScan,{ex[i]})
      ex2=ex2 & and_bits(#FF,word)  -- low byte
      ex3=ex3 & floor(word/256) -- high byte
  end for
  shift_out(VK_LWIN,RunDialog)
  key_out(VK_BACK)
  sleep(1)
  for i = 1 to length(ex2) do
  
    if ex3[i] = 0 then
        key_out(ex2[i])
    elsif ex3[i] = 1 then
        shift_out(VK_SHIFT,ex2[i])
    else 
        abort(0) -- escape if CTRL or ALT key.
    end if
  
  end for
  sleep(1)
  key_out(VK_RETURN)

end procedure
setHandler( Button4, w32HClick, routine_id("run_dialog") )


procedure program_menu( integer self, integer event, sequence params )
  shift_out(VK_LWIN,StartMenu)
  key_out(VK_RETURN) -- go right to programs ?
  shift_out(VK_SHIFT,'P')
end procedure
setHandler( Button5, w32HClick, routine_id("program_menu") )


procedure about( integer self, integer event, sequence params )
  jk = message_box("'tutorial' demo, thanks to VB\n" & 
                   "... that's Visual Basic  ;-)",
                   "...another", 0 )
end procedure
setHandler( About, w32HClick, routine_id("about") )


procedure leave( integer self, integer event, sequence params )
	closeWindow(Win1)
end procedure
setHandler( Leave, w32HClick, routine_id("leave") )

WinMain(Win1,Normal)
