Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting Ctrl+<letter> in KeyPress

Status
Not open for further replies.

IRABYY

Programmer
Apr 18, 2002
221
US
Colleagues:

Have any of you tried to "intercept" Ctrl+C, or Alt+F, or some other "reserved" key combinations (i.e. Ctrl+F) in the FORM.KeyPress procedure, provided FORM.KeyPreview = .T.?

I tried it - and could not make it work.

Here's how I tested it:
1. I made a blank form, set KeyPreview to True.
2. In the FORM.KeyPress procedure I entered single line of code:
Code:
WAIT WINDOW NOWAIT [ nKeyCode = ] + ;
                   ALLTRIM(STR(nKeyCode, 3, 0)) + ;
                   [, nShiftAltCtrl = ] + ;
                   STR(nShiftAltCtrl, 1, 0)
3. Ran that form, pressed Ctrl+C - no wait window; Ctrl+F - same thing; Shift+<letter> - any letter works (invokes wait window).

For that matter, none of Alt+<letter> combinations works at all. Most likely, all Alt+<letter> are reserved for shortcuts. The problem is that I had to make the buttons in my toolbox (VFP's Command Group control) so small that only an icon fits in a button, no space for caption whatsoever. Therefore, I need to have my own procedures for Alt+<letter>.

Apparently, combinations like Ctrl+C, or Alt+<letter> are reserved for either OS or VFP itself.

Is there any way around?

AHWBGA


Regards,

Ilya
 
IRABYY

You need the WinAPI call GetKeyState()

#define VK_LBUTTON 0x01
#define VK_RBUTTON 0x02
#define VK_CANCEL 0x03
#define VK_MBUTTON 0x04 && NOT contiguous with L & RBUTTON

#define VK_BACK 0x08
#define VK_TAB 0x09

#define VK_CLEAR 0x0C
#define VK_RETURN 0x0D

#define VK_SHIFT 0x10
#define VK_CONTROL 0x11
#define VK_MENU 0x12
#define VK_PAUSE 0x13
#define VK_CAPITAL 0x14

#define VK_ESCAPE 0x1B

#define VK_SPACE 0x20
#define VK_PRIOR 0x21
#define VK_NEXT 0x22
#define VK_END 0x23
#define VK_HOME 0x24
#define VK_LEFT 0x25
#define VK_UP 0x26
#define VK_RIGHT 0x27
#define VK_DOWN 0x28
#define VK_SELECT 0x29
#define VK_PRINT 0x2A
#define VK_EXECUTE 0x2B
#define VK_SNAPSHOT 0x2C
#define VK_INSERT 0x2D
#define VK_DELETE 0x2E
#define VK_HELP 0x2F

* VK_0 through VK_9 are the same as ASCII '0' through '9' (0x30 - 0x39)
* VK_A through VK_Z are the same as ASCII 'A' through 'Z' (0x41 - 0x5A)

#define VK_LWIN 0x5B
#define VK_RWIN 0x5C
#define VK_APPS 0x5D

#define VK_NUMPAD0 0x60
#define VK_NUMPAD1 0x61
#define VK_NUMPAD2 0x62
#define VK_NUMPAD3 0x63
#define VK_NUMPAD4 0x64
#define VK_NUMPAD5 0x65
#define VK_NUMPAD6 0x66
#define VK_NUMPAD7 0x67
#define VK_NUMPAD8 0x68
#define VK_NUMPAD9 0x69
#define VK_MULTIPLY 0x6A
#define VK_ADD 0x6B
#define VK_SEPARATOR 0x6C
#define VK_SUBTRACT 0x6D
#define VK_DECIMAL 0x6E
#define VK_DIVIDE 0x6F
#define VK_F1 0x70
#define VK_F2 0x71
#define VK_F3 0x72
#define VK_F4 0x73
#define VK_F5 0x74
#define VK_F6 0x75
#define VK_F7 0x76
#define VK_F8 0x77
#define VK_F9 0x78
#define VK_F10 0x79
#define VK_F11 0x7A
#define VK_F12 0x7B
#define VK_F13 0x7C
#define VK_F14 0x7D
#define VK_F15 0x7E
#define VK_F16 0x7F
#define VK_F17 0x80
#define VK_F18 0x81
#define VK_F19 0x82
#define VK_F20 0x83
#define VK_F21 0x84
#define VK_F22 0x85
#define VK_F23 0x86
#define VK_F24 0x87

#define VK_NUMLOCK 0x90
#define VK_SCROLL 0x91

DO CASE
CASE GetKeyState(VK_SHIFT) < 0 OR ;
[tab]GetKeyState(VK_SHIFT) > 1
[tab]WAIT WIND [Shift key pressed]
CASE GetKeyState(VK_CONTROL) < 0 OR ;
[tab]GetKeyState(VK_CONTROL) > 1
[tab]WAIT WIND [Control key pressed]
CASE GetKeyState(VK_MENU) < 0 OR ;
[tab]GetKeyState(VK_MENU) > 1
[tab]WAIT WIND [Alt key pressed]
ENDC

FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
IRABYY

I omitted this - it enables you to differentiate between left and right keys

* VK_L* & VK_R* - left and right Alt, Ctrl and Shift virtual keys.
* Used only as parameters to GetAsyncKeyState() and GetKeyState().
* No other API or message will distinguish left and right keys in this way.

#define VK_LSHIFT 0xA0
#define VK_RSHIFT 0xA1
#define VK_LCONTROL 0xA2
#define VK_RCONTROL 0xA3
#define VK_LMENU 0xA4
#define VK_RMENU 0xA5



FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Ilya,

The reason it didn't work in your test is that you chose certain key combinations that have special meanings in the development environment.

Ctrl-C copies to the clipboard; Ctrl-F brings up the Find dialogue; other Ctrl combos do similar things, like Ctrl-A selects all text.

If you had tested with Ctrl-Q, -W, -T, -K, etc. it would have worked.

I think that it should work fine if you build an EXE and run it outside VFP. Perhaps you could try and let us know.

Mike


Mike Lewis
Edinburgh, Scotland
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top