KeyPress aber auch, wenn etwas anderes Focusiert ist



  • Hallo,

    ich habe jetzt mal beispiel haft folgenden Code:

    private: System::Void Form1_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
    				e->Handled = true;
    
    				//Pfeiltaste: OBEN
    					if(e->KeyCode == Keys::Up) {
    							Close();
    					}
    				//Pfeiltaste: UNTEN
    					if(e->KeyCode == Keys::Down) {
    							Close();
    					}
    				//Pfeiltaste: RECHTS
    					if(e->KeyCode == Keys::Right) {
    							Close();
    					}
    				//Pfeiltaste: LINKS
    					if(e->KeyCode == Keys::Left) {
    							Close();
    					}
    	}
    

    Mein Problem ist das ich auf meiner Form, Steuerelemente habe, nun will ich aber nicht für jedes Element eine "KeyPress" Methode erstellen, sondern will das z.B. das der Code auf der Form und auf allen Button Elementen ausgeführt wird, und auf den anderen einfach ignoriert, gibt es eine möglichkeit dies zu realisieren.



  • Schau Dir mal KeyPreview an



  • Aber wenn ich es so mache, passiert auch nicht:

    private: System::Void Form1_PreviewKeyDown(System::Object^  sender, System::Windows::Forms::PreviewKeyDownEventArgs^  e) {
    				//Pfeiltaste: OBEN
    					if(e->KeyCode == Keys::Up) {
    						Close();
    					}
    				//Pfeiltaste: UNTEN
    					if(e->KeyCode == Keys::Down) {
    						Close();
    					}
    				//Pfeiltaste: RECHTS
    					if(e->KeyCode == Keys::Right) {
    						Close();
    					}
    				//Pfeiltaste: LINKS
    					if(e->KeyCode == Keys::Left) {
    						Close();
    					}
    		 }
    




  • Da ist was mit einem bool wert, aber ich verstehe das jetzt nicht genau.



  • Hab es nun mit folgendem Beispiel versucht:

    // 
    			// Form1
    			// 
    			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
    			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    			this->ClientSize = System::Drawing::Size(803, 599);
    			this->Controls->Add(this->listBox1);
    			this->Controls->Add(this->groupBox4);
    			this->Controls->Add(this->groupBox3);
    			this->Controls->Add(this->tabControl2);
    			this->Controls->Add(this->tabControl1);
    			this->Controls->Add(this->statusStrip1);
    			this->Controls->Add(this->groupBox2);
    			this->Controls->Add(this->groupBox1);
    			this->Controls->Add(this->menuStrip1);
    			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
    			this->Icon = (cli::safe_cast<System::Drawing::Icon^  >(resources->GetObject(L"$this.Icon")));
    			this->MainMenuStrip = this->menuStrip1;
    			this->MaximizeBox = false;
    			this->Name = L"Form1";
    			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
    			this->Text = L"Robot Controller";
    			this->FormClosed += gcnew System::Windows::Forms::FormClosedEventHandler(this, &Form1::Form1_FormClosed);
    			this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::Form1_KeyDown);
    			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
    			this->groupBox1->ResumeLayout(false);
    			this->groupBox2->ResumeLayout(false);
    			this->menuStrip1->ResumeLayout(false);
    			this->menuStrip1->PerformLayout();
    			this->statusStrip1->ResumeLayout(false);
    			this->statusStrip1->PerformLayout();
    			this->tabControl1->ResumeLayout(false);
    			this->tabControl2->ResumeLayout(false);
    			this->tabPage3->ResumeLayout(false);
    			this->groupBox5->ResumeLayout(false);
    			this->groupBox5->PerformLayout();
    			this->groupBox6->ResumeLayout(false);
    			this->groupBox6->PerformLayout();
    			this->tabPage4->ResumeLayout(false);
    			this->groupBox3->ResumeLayout(false);
    			this->groupBox3->PerformLayout();
    			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->EndInit();
    			this->ResumeLayout(false);
    			this->PerformLayout();
    			this->KeyPreview = true;
    
    		}
    #pragma endregion
    
    //Manuelle Steuerung über Tastatur
    	private: System::Void Form1_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
    				listBox1->Items->Add( e->KeyCode );
    				e->Handled = false;
    
    	}
    

    Doch leider passiert nicht falls etwas anderes Focusiert ist.





  • Geht das nur mit KeyPress? Den ich brauche KeyDown und KeyUp..



  • Ausprobieren und Hirneinschalten könnte helfen.



  • Ach, ne sorry, hab das nur gefragt da falls etwas focuiert ist und man die Pfeiltasten drückt nichts passiert. Ich brauche sie aber, wie soll ich das dann machen?



  • Mann, alter...

    Click mal auf Knuddelbaers MSDN Link und scroll ein wenig runter...

    Da steht:

    true if the form will receive all key events; false if the currently selected control on the form receives key events. The default is false.

    Was wollen uns die Leute von Microsoft nur damit sagen??? 😉


Anmelden zum Antworten