Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Sending keystrokes to non VFP windows
Message
General information
Forum:
Visual FoxPro
Category:
Windows API functions
Miscellaneous
Thread ID:
00158751
Message ID:
00160828
Views:
33
You just need to call keybd_event with the virtual key codes for the keys that you want pressed/depressed. For example, Ctrl+F2 will be:

#define VK_F2 0x71
#define VK_CONTROL 0x11
= keybd_event(VK_CONTROL, 0, 0, 0)
= keybd_event(VK_F2, 0, 0, 0)
= keybd_event(VK_F2, 0, KEYEVENTF_KEYUP, 0)
= keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0)

The numeric pad "5" will be:
#define VK_NUMPAD5 0x65
= keybd_event(VK_NUMPAD5, 0, 0, 0)
= keybd_event(VK_NUMPAD5, 0, KEYEVENTF_KEYUP, 0)

Vlad

>How would you pass extended key codes or ctrl sequences here? Use
>the appropriate DEFINEs?
>
>+++ Rick ---
>
>>>Here's a version that is a little improved and doesn't have the problem with the CAPS key. BTW, don't send the char's ascii code directly as virtual key code. Use VkKeyScan() WIn32API function to convert to virtual key code and get the status of the shift key.
>>>
>>>
#DEFINE KEYEVENTF_KEYUP      2
>>>#DEFINE VK_SHIFT          0x10
>>>#define VK_CAPITAL        0x14
>>>DECLARE SHORT VkKeyScan IN Win32API ;
>>>	INTEGER nChar
>>>DECLARE INTEGER SetForegroundWindow IN Win32API;
>>>	INTEGER hWnd
>>>DECLARE INTEGER SetActiveWindow IN Win32API;
>>>	INTEGER hWnd
>>>DECLARE keybd_event IN Win32API;
>>>	INTEGER bVk, INTEGER bScan,;
>>>	INTEGER dwFlags, INTEGER dwExtraInfo
>>>DECLARE INTEGER ShowWindow IN Win32API;
>>>	INTEGER hWnd, INTEGER uShow
>>>DECLARE INTEGER SetFocus IN Win32API AS SetKeybdFocus;
>>>	INTEGER hWnd
>>>lcsentence = "THE quick brown fox jumped over the lazy dogs"
>>>lnhWnd = Is_Run32("Notepad")
>>>clear
>>>IF lnhWnd > 0
>>>	= ShowWindow(lnhWnd, 9)
>>>	= SetActiveWindow(lnhWnd)
>>>	= SetForegroundWindow(lnhWnd)
>>>	= SetKeybdFocus(lnhWnd)
>>>	*-- Store the status of the CAPS key
>>>	llCapsWasOn = CAPSLOCK()
>>>	IF llCapsWasOn
>>>		*-- If CAPS is on, turn it off.
>>>		= keybd_event(VK_CAPITAL, 0, 0, 0)
>>>		= keybd_event(VK_CAPITAL, 0, KEYEVENTF_KEYUP, 0)
>>>	ENDIF
>>>	FOR lni = 1 TO LEN(lcsentence)
>>>		lcchar = SUBSTR(lcsentence, lni, 1)
>>>		*-- Get the virtual key code and the shift status
>>>		*   for the current char.
>>>		lnVKCodeShiftState = VkKeyScan(ASC(lcchar))
>>>		
>>>		*-- Extract the virtual key code
>>>		lnVKCode = MOD(lnVKCodeShiftState, 256)
>>>		
>>>		*-- Extract the shift status.
>>>		*   (Pressed if the bit 0 of the second byte is 1.)
>>>		*   The status of the ALT and CTRL keys can
>>>		*   be controled in the same way but I don't
>>>		*   do it in this example...
>>>		lnShiftCode = INT(lnVKCodeShiftState / 256)
>>>		llShift = BITTEST(lnShiftCode, 0)
>>>		
>>>		IF llShift
>>>			= keybd_event(VK_SHIFT, 0, 0, 0)
>>>		ENDIF
>>>		= keybd_event(lnVKCode, 0, 0, 0)
>>>		= keybd_event(lnVKCode, 0, KEYEVENTF_KEYUP, 0)
>>>		IF llshift
>>>			= keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0)
>>>		ENDIF
>>>	NEXT
>>>	*-- Restore the status of the CAPS key
>>>	IF llCapsWasOn
>>>		= keybd_event(VK_CAPITAL, 0, 0, 0)
>>>		= keybd_event(VK_CAPITAL, 0, KEYEVENTF_KEYUP, 0)
>>>	ENDIF
>>>ENDIF
>>>
>>>
>>Hi Vlad,
>>
>>Looks pretty good. Mark does make a good point, however, regarding sending it to the clipboard. It certainly would seem to be easier just to _CLIPTEXT = lcsentence, then paste. I still think that SendMessage() might be better than using the keybd_event(). However, there's some good information here that I can certainly make use of. Thanks.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform