Ruby 获取键盘输入
1. >>>>>>>>>># 
2. #Main Application Code Start 
3. >>>>>>>>>># 
4.  
5. puts "---begin---" 
6.  
7. #display msgbox to ask user to input from keyboard 
8. MessageBox = Win32::API.new("MessageBox"'LPPI''I'"user32"
9. MessageBox.call(0, "Please input 1 to switch to PRT1 or 2 to switch to PRT2""MsgBox", 0) 
10.  
11. #get input from keyboard 
12. char = getchar 
13. if char==49   #if input is 1, 49 is ASC for 1 
14.   prt = "PRT1" 
15.   puts "---Will switch to PRT1!---" 
16. elsif char==50    #if input is 2, 50 is ASC for 2 
17.   prt = "PRT2" 
18.   puts "---Will switch to PRT2!---" 
19. else    #error handling if input is not 1 or 2 
20.   raise RuntimeError, "bad argument" 
21. end 
22.  
23. #get input for pool name from keyboard 
24. box_name = gets.chomp 
25. puts box_name 
26. box_name = "fp"+box_name 
27. puts box_name 
28. #MessageBox.call(4,"You want to operate "+box_name,"Is it OK?",3) 
29. puts box_name+" pool is selected!" 
30.  
31. #start to open ie to config page 
32. $ie = Watir::IE.new_process 
33. $ie.set_fast_speed 
34. $ie.goto(CONFIG_URL) 
35.  
36. #refresh page and try to find right config link 
37. $ie.select_list(:name"box").select(box_name) 
38. $autoit = WIN32OLE.new('AutoItX3.Control'
39. puts "---refresh---" 
40. sleep 2 
41. $autoit.Send('{F5}')#refresh the page 
42. if char == 49  #config to PRT1 
43.   i = 4 
44. else    #config to PRT2autoit 
45.   i = 5 
46. end 
47.  
48. while i>0 do 
49.      $autoit.Send('{TAB}'
50.      sleep 1   
51.      i=i-1 
52. end 
53. $autoit.Send('{ENTER}')   
54. puts "Setting is&" 
55. sleep 10 
56.  
57. shell = WIN32OLE.new('Shell.Application'
58. windows = shell.Windows 
59. i = 0 
60. windows.each {|w| puts&_s + " " + w.LocationURL; i+=1 } 
61. if i ==2 
62.   #puts "---Successfully Set "+POOL_NAME+" to PRT1---" 
63.   MessageBox.call(0, box_name+" switch to "+prt+" successfully!""MsgBox", 0) 
64. else 
65.   MessageBox.call(0, box_name+" Switch to "+prt+" Failed!""MsgBox", 0) 
66. end