フット入力 > Winソフト(v0.2) > 追加 > リスト選択のキー転送

http://qiita.com/7of9/items/5e3fc335da77d57591fc
の続き

## ソフト内容

コンボボックスから選択したキーをフット入力で入力できること

![DSC_0163.JPG](https://qiita-image-store.s3.amazonaws.com/0/32870/255c1fa1-016f-f5d1-9bff-6063ce7bd038.jpeg)

![qiita.png](https://qiita-image-store.s3.amazonaws.com/0/32870/08ebf96b-2d94-d188-0928-3c40852410e4.png)


基本機能は実装完了。

Sublime Textで使う`Ctrl+Shift+F`は左足Ctrl, 右足Shift, 左手Fのコンボで使えば良かった。1つのフット入力でキーコンボを転送するよりは柔軟性がある。

なお、この記事を書きながらフット入力を使い始めている。Baskcpace, PgUp, PgDnなど。

## code (v0.2)

```Main.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include <memory>
#include "Main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------

/*
v0.2 2016 Mar. 1
	- add assignKeyCode()
	- add keyItemString
	- add isAlternateKey()
	- add keyTbl[]
v0.1 2016 Apr 24
	- recognize rising edge (push)
	- receive inputs through UDP
*/

static const int kNumInput = 6;

static bool preInputs[kNumInput] = { 0 };

#define VK_F (0x46)

static const uint8_t vkKeyTbl[] = {
	VK_CONTROL,
	VK_SPACE,
	VK_SHIFT,
	VK_BACK,
	VK_RETURN,
	VK_DIVIDE,
	VK_PRIOR, // PgUp
	VK_NEXT, // PgDn
};

static const String keyItemString =
	L"CONTROL"
	L",SPACE"
	L",SHIFT"
	L",BACKSPACE"
	L",RETURN"
	L",/"
	L",PgUp"
	L",PgDn"
;

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
	Timer1->Enabled = false;

	IdUDPClient1->Host = E_ipadr->Text;
	IdUDPClient1->Port = 7002;
	IdUDPClient1->Active = true;

	IdUDPClient1->Send("foot\r\n");

	Sleep(10);
	Application->ProcessMessages();

	DWORD start = GetTickCount();
	DWORD elapsed;

	AnsiString res = IdUDPClient1->ReceiveString(300);

	elapsed = GetTickCount() - start;

	procInput(res);

	IdUDPClient1->Active = false;
	Timer1->Enabled = true;

}
//---------------------------------------------------------------------------

static bool isAlternateKey(int8_t vkkey)
{
	if (vkkey == VK_SHIFT || vkkey == VK_CONTROL) {
		return true;
	}
	return false;
}

bool TForm1::assignKeyCode(int SWidx, int8_t *dstPtr)
{
	int itmidx = -1;

	if (dstPtr == NULL) {
		return false;
	}

	switch(SWidx) {
	case 0:
		itmidx = CB_KeyAssign1->ItemIndex;
		break;
	case 1:
		itmidx = CB_KeyAssign2->ItemIndex;
		break;
	case 2:
		itmidx = CB_KeyAssign3->ItemIndex;
		break;
	case 3:
		itmidx = CB_KeyAssign4->ItemIndex;
		break;
	case 4:
		itmidx = CB_KeyAssign5->ItemIndex;
		break;
	case 5:
		itmidx = CB_KeyAssign6->ItemIndex;
		break;
	}

	if (itmidx < 0) {
		return false;
	}

	*dstPtr = vkKeyTbl[itmidx];
	return true;
}

void __fastcall TForm1::procInput(String res)
{
	if(res.Length() == 0) {
        return;
	}

	std::unique_ptr<TStringList> line(new TStringList);

	line->CommaText = res;

	bool ins[kNumInput];
	try {
		for(int idx=0; idx < kNumInput; idx++) {
			ins[idx] = !( (bool)line->Strings[idx+1].ToInt() );
		}
	} catch (...) {
	}

	CheckBox1->Checked = ins[0];
	CheckBox2->Checked = ins[1];
	CheckBox3->Checked = ins[2];
	CheckBox4->Checked = ins[3];
	CheckBox5->Checked = ins[4];
	CheckBox6->Checked = ins[5];

	int8_t kycd;
	bool resOK;

	for(int idx=0; idx < kNumInput; idx++) {
		if (preInputs[idx] == ins[idx]) {
			continue;
		}

		resOK = assignKeyCode(idx, &kycd);
		if (resOK == false) {
			continue;
		}

		if (ins[idx]) {
			keybd_event(kycd, 0, 0, 0);
		} else {
			if (isAlternateKey(kycd)) {
				keybd_event(kycd, 0, KEYEVENTF_KEYUP, 0);
			}
		}

		preInputs[idx] = ins[idx];
	}
}

void __fastcall TForm1::FormShow(TObject *Sender)
{
	CB_KeyAssign1->Items->CommaText = keyItemString;
	CB_KeyAssign2->Items->CommaText = keyItemString;
	CB_KeyAssign3->Items->CommaText = keyItemString;
	CB_KeyAssign4->Items->CommaText = keyItemString;
	CB_KeyAssign5->Items->CommaText = keyItemString;
	CB_KeyAssign6->Items->CommaText = keyItemString;
}
//---------------------------------------------------------------------------
```

## TODO

- 設定保存機能
- UDP通信ON/OFF機能