.net - Visual C++ Watch for key sequence -
if have form application in c++, how set watches specific key sequence perform action? like, instance, watching user tap arrow keys in specific order, , when happens open form?
(aparrantly .net? i'm new doing forms, i'm little lost here.)
as long you're okay requiring main form
have focus, simple version involves setting keypreview
property true
, adding handler previewkeydown
:
#using <system::windows::forms.dll> // brevity of example using namespace system; using namespace system::windows::forms; public ref class form1 : form { private: // desired key sequence static array<keys>^ contra = { keys::up, keys::up, keys::down, keys::down, keys::left, keys::right, keys::left, keys::right, keys::b, keys::a, keys::launchapplication1}; // how far sequence user int keyseqpos; // other data int lives; void form1_previewkeydown(object^ sender, previewkeydowneventargs^ e) { if{keyseqpos < 0 || keyseqpos >= contra->length) { // reset position if it's invalid keyseqpos = 0; } // use following test if don't care modifiers (ctrl, alt, shift) // otherwise can test direct equality: e->keycode == contra[keyseqpos] // caps lock, num lock, , scroll lock harder deal if(e->keycode & keys.keycode == contra[keyseqpos]) { keyseqpos++ } else { keyseqpos = 0; // alternatively, keep history , check see if // suffix of matches prefix of code, setting keyseqpos // length of match } if(keyseqpos == contra->length) { // key sequence complete, whatever want lives += 3; } } void initializecomponent() { // other initialization code this->keypreview = true; this->previewkeydown += gcnew previewkeydowneventhandler(this, &form1::form1_previewkeydown); } public: form1() { initializecomponent(); keyseqpos = 0; lives = 3; } }
Comments
Post a Comment