October 28, 2020, 05:05:55 AM

Author Topic: [bb] Virtual Keys by Eikon [ 1+ years ago ]  (Read 1246 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Virtual Keys by Eikon [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Virtual Keys
Author : Eikon
Posted : 1+ years ago

Description : Virtual Keys is a function which handles the simulation of keypresses. If you have ever needed to force a keystroke, this program is for you. In the included sample, a file is read and then typed out automatically in notepad.

Though the VKey function works in both B+ and B3D, this sample uses a Blitz+ frontend.

A text file, "file.txt", is required. Place in this file whatever text you want the program to type into notepad.

Grab the <a href="http://s93151849.onlinehome.us/eik/vkey.zip" target="_blank">project[/url]


Code :
Code: BlitzBasic
  1. ; Userlib
  2. ;.lib "user32.dll"
  3. ;keypress%(bVk%, bScan%, dwFlags%, dwExtraInfo%):"keybd_event"
  4. ;SetWindowPos%(hWnd%,hAfter%,x%,y%,cx%,cy%,flags%)
  5. ;GetActiveWindow%()
  6.  
  7. ; Virtual Keys 1.1 by Eikon
  8. ; eikon@carolina.rr.com
  9. ; Last revised 12/09/04
  10. Local DeskW = ClientWidth(Desktop()), DeskH = ClientHeight(Desktop())
  11. Win = CreateWindow("Virtual Keys", DeskW / 2 - 150, DeskH / 2 - 60, 300, 120, 0, 1)
  12. hwnd% = GetActiveWindow()
  13. Font = LoadFont("System", 10, 0, 0, 0)
  14. Button = CreateButton("GO!", 6, 55, 282, 32, Win, 1): SetGadgetFont Button, Font
  15. msg$ = "Click on the button to fire up Notepad."
  16. msg$ = msg$ + " Do not close Notepad until it's finished typing."
  17. Label = CreateLabel(msg$, 7, 10, 280, 40, Win, 0)
  18. SetGadgetFont Label, Font
  19.  
  20. Repeat
  21.  
  22. Select WaitEvent()
  23.        
  24.         Case $803: End
  25.         Case $401
  26.         If EventSource() = Button Then ; Begin
  27.                 SetWindowPos hwnd, -2, 0, 0, 0, 0, 1 + 2     ; Push window under notepad
  28.                 ExecFile Chr$(34) + "notepad.exe" + Chr$(34) ; Shell notepad
  29.                 Delay 500                                    ; Give it time to come up
  30.                 File = ReadFile("file.txt")                  ; Begin reading from file
  31.                 Repeat
  32.                         tmp$ = ReadLine(File)
  33.                                 For i = 1 To Len(tmp$)
  34.                                         vkey Mid(tmp$, i, 1), 1          ; Press the key
  35.                                         Delay 5                          ; Small delay to slow typing speed
  36.                                 Next
  37.                         vkey "ret", 1                                                    ; Return
  38.                 Until Eof(File)
  39.                 CloseFile File                               ; Close file
  40.                
  41.                 ; Begin some trickery
  42.                 vkey "alt", 0 ; Hold ALT
  43.                 vkey "f", 1   ; Press F
  44.                 vkey "alt", 2 ; Release ALT
  45.                 For i = 1 To 4 ; Move right
  46.                         vkey "right", 1: Delay 150
  47.                 Next
  48.                 For i = 1 To 4 ; Move left
  49.                         vkey "left", 1: Delay 150
  50.                 Next
  51.                 For i = 1 To 6 ; Move down to exit
  52.                         vkey "down", 1: Delay 150
  53.                 Next
  54.                 vkey "ret", 1   ; Press return
  55.                 vkey "right", 1: Delay 500 ; Go to "no"
  56.                 vkey "ret", 1   ; Exit notepad without saving
  57.                
  58.         EndIf
  59.                
  60. End Select     
  61. Forever
  62.  
  63. ; //===================================================
  64. ; // Virtual Keys VKEY Function
  65. ; //
  66. ; // Char$  - String containing the character to be pressed
  67. ; // State% - 0 KeyDown
  68. ; //        - 1 Keypress
  69. ; //        - 2 KeyUp
  70. ; //
  71. ; // Special Char Codes
  72. ; //
  73. ; // RET   - Return
  74. ; // ALT   - ALT Key
  75. ; // UP    - Cursor key up
  76. ; // DOWN  - Cursor key down
  77. ; // LEFT  - Cursor key left
  78. ; // RIGHT - Cursor key right
  79. ; // F1 through F12 - Function Keys
  80. ; // NUM   - Num lock
  81. ; // SCR   - Scroll lock
  82. ; // LCTRL - Left control
  83. ; // RCTRL - Right control
  84. ; // LSH   - Left shift
  85. ; // RSH   - Right shift
  86. ; // LWIN  - Left Windows key
  87. ; // RWIN  - Right Windows key
  88. ; // DEL   - Delete
  89. ; // INS   - Insert
  90. ; // PRN   - Print screen
  91. ; // HOME  - Home
  92. ; // END   - End
  93. ; // ESC   - Escape
  94. ; // LMB   - Left mouse button
  95. ; // RMB   - Right mouse button
  96. ; // MMB   - Middle mouse button
  97. ; // BACK  - Backspace
  98. ; // TAB   - Tab
  99. ; // NEXT  - Page down
  100. ; // PRIOR - Page up
  101. ; // Chr$(34) - Double Quotes
  102. ; //===============================================================
  103. Function vkey(char$, state%)
  104. tmp = 0: shift = False
  105.  
  106. ; Shift for uppercase
  107. If Lower$(char$) <> char$ Or char$ = Chr$(34) Then
  108.         keypress 161, 0, 0, 0 ; Hold shift
  109.         shift = True
  110. EndIf
  111.  
  112. char$ = Lower$(char$)
  113. Select char$
  114.         Case "0": tmp = 48
  115.         Case "1": tmp = 49
  116.         Case "2": tmp = 50
  117.         Case "3": tmp = 51
  118.         Case "4": tmp = 52
  119.         Case "5": tmp = 53
  120.         Case "6": tmp = 54
  121.         Case "7": tmp = 55
  122.         Case "8": tmp = 56
  123.         Case "9": tmp = 57
  124.         Case "a": tmp = 65
  125.         Case "b": tmp = 66
  126.         Case "c": tmp = 67
  127.         Case "d": tmp = 68
  128.         Case "e": tmp = 69
  129.         Case "f": tmp = 70
  130.         Case "g": tmp = 71
  131.         Case "h": tmp = 72
  132.         Case "i": tmp = 73
  133.         Case "j": tmp = 74
  134.         Case "k": tmp = 75
  135.         Case "l": tmp = 76
  136.         Case "m": tmp = 77
  137.         Case "n": tmp = 78
  138.         Case "o": tmp = 79
  139.         Case "p": tmp = 80
  140.         Case "q": tmp = 81
  141.         Case "r": tmp = 82
  142.         Case "s": tmp = 83
  143.         Case "t": tmp = 84
  144.         Case "u": tmp = 85
  145.         Case "v": tmp = 86
  146.         Case "w": tmp = 87
  147.         Case "x": tmp = 88
  148.         Case "y": tmp = 89
  149.         Case "z": tmp = 90
  150.         Case " ": tmp = 32
  151.         Case ".": tmp = 190
  152.         Case ",": tmp = 188
  153.         Case "'": tmp = 222
  154.         Case Chr$(34): tmp = 222
  155.         Case ";": tmp = 186
  156.         Case "/": tmp = 191
  157.         Case "": tmp = 220
  158.         Case "-": tmp = 189
  159.         Case "ret": tmp = 13
  160.         Case "alt": tmp = 18
  161.         Case "down": tmp = 40
  162.         Case "right": tmp = 39
  163.         Case "up": tmp = 38
  164.         Case "left": tmp = 37
  165.         Case "f1": tmp = 112
  166.         Case "f2": tmp = 113
  167.         Case "f3": tmp = 114
  168.         Case "f4": tmp = 115
  169.         Case "f5": tmp = 116
  170.         Case "f6": tmp = 117
  171.         Case "f7": tmp = 118
  172.         Case "f8": tmp = 119
  173.         Case "f9": tmp = 120
  174.         Case "f10": tmp = 121
  175.         Case "f11": tmp = 122
  176.         Case "f12": tmp = 123
  177.         Case "num": tmp = 144
  178.         Case "scr": tmp = 145
  179.         Case "lctrl": tmp = 162
  180.         Case "rctrl": tmp = 163
  181.         Case "lwin": tmp = 91
  182.         Case "rwin": tmp = 92
  183.         Case "del": tmp = 46
  184.         Case "ins": tmp = 45
  185.         Case "prn": tmp = 44
  186.         Case "home": tmp = 36
  187.         Case "end": tmp = 35
  188.         Case "esc": tmp = 27
  189.         Case "lmb": tmp = 1
  190.         Case "rmb": tmp = 2
  191.         Case "mmb": tmp = 4
  192.         Case "back": tmp = 8
  193.         Case "tab": tmp = 9
  194.         Case "next": tmp = 34
  195.         Case "prior": tmp = 33
  196.        
  197. End Select
  198.  
  199. If tmp <> 0 Then
  200.         If state = 0 Then keypress tmp, 0, 0, 0 ; Down
  201.         If state = 1 Then keypress tmp, 0, 0, 0: keypress tmp, 0, 2, 0 ; Press
  202.         If state = 2 Then keypress tmp, 0, 2, 0 ; Up
  203. EndIf
  204. If shift = True Then keypress 161, 0, 2, 0 ; Release shift
  205. End Function


Comments :


Conan(Posted 1+ years ago)

 `You gonna make the b3d anytime soon?


Eikon(Posted 1+ years ago)

 The function already works in B3D. Just rip out the GUI code and it will compile fine.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal