Tutorial 8 : Detecting Asynchronous Keys Using Visual C++.NET 2010, 2008
This project will allow you to move a label around your form using Asynchronous Keys. Click here for the source code to the Asynchronous Keys Program in Visual C++.NET for the 2010 version or click here for the 2008 version of the source and the project. It is called asynchronous because more than one key can be pressed at a time and it will be recognized. This is usually something that isn't allowed. First set up your form to look as follows (and yes a timer needs to be added). You must include the dynamic link library user32.dll for this program to allow the asynchronous reading of key presses.
Enable the timer.. Next code the following, add everything in bright red:
This project will allow you to move a label around your form using Asynchronous Keys. Click here for the source code to the Asynchronous Keys Program in Visual C++.NET for the 2010 version or click here for the 2008 version of the source and the project. It is called asynchronous because more than one key can be pressed at a time and it will be recognized. This is usually something that isn't allowed. First set up your form to look as follows (and yes a timer needs to be added). You must include the dynamic link library user32.dll for this program to allow the asynchronous reading of key presses.
#pragma once
#include<windows.h>
#include<time.h>
#pragma comment(lib, "user32")
namespace AsynchronousKeys {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
publicrefclass Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0 )
InitializeComponent();
}
protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Timer^ timer1;
private: System::ComponentModel::IContainer^ components;
protected:
private:
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->label1 = (gcnew System::Windows::Forms::Label());
this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
this->SuspendLayout();
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(144, 29);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(35, 13);
this->label1->TabIndex = 0;
this->label1->Text = L"label1";
this->timer1->Enabled = true;
this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 105);
this->Controls->Add(this->label1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e)
{
if (KEY_DOWN(VK_LEFT))
label1->Text="left";
if (KEY_DOWN(VK_RIGHT))
label1->Text="right";
if (KEY_DOWN(VK_UP))
label1->Text="up";
if (KEY_DOWN(VK_DOWN))
label1->Text="down";
if (KEY_DOWN(VK_SPACE))
label1->Text="space";
if (KEY_DOWN(VK_ESCAPE))
label1->Text="escape";
if (KEY_DOWN(65))
label1->Text="A";
if (KEY_DOWN(90))
label1->Text="Z";
}
};
}
|
0 comments:
Post a Comment