keyboard = {}

local valueBetween = function(minX, maxX, minY, maxY, currentMouseX, currentMouseY)

	if ((currentMouseX >= minX and currentMouseX <= maxX) and (currentMouseY >= minY and currentMouseY <= maxY)) then
		return true;
	end
	
	return false;
end

--[[ If you need a Japanese keyboard for your lua application and
the return value should be UTF-8, call this function from your application like this 
inputtedText = showKeyboard(). With dofile("keyboard.lua") you can take this
"library" to be used with your LuaPlayer Euphoria V8 application. However, you need
to modify this function a little. Remove home, select and start pads and copy the
required colors and ifont codes from index.lua. There are also a few global variables used.]]
function showKeyboard()
	  screen.waitVblankStart(20);
	  local searchWord = "";
	  local searchArray = {}
	  
	  local x=25;
	  local y=50;
	  
	  while true do
	  
		System.draw();
		screen:clear()
	  
		ifontJpn:setStyle(1.0, red, darkgray, IntraFont.ALIGN_CENTER);
		ifontJpn:print(240, 20, searchWord)
		
		ifontJpn:setStyle(1.0, lime, darkgray, IntraFont.ALIGN_CENTER);
		
		if (hiraganaMode) then
		
		x=25
		y=50;
	  
		ifontJpn:print(x*1, y, "あ");
		ifontJpn:print(x*2, y, "い");
		ifontJpn:print(x*3, y, "う");
		ifontJpn:print(x*4, y, "え");
		ifontJpn:print(x*5, y, "お");
		ifontJpn:print(x*7, y, "ぁ");
		ifontJpn:print(x*8, y, "ぃ");
		ifontJpn:print(x*9, y, "ぅ");
		ifontJpn:print(x*10, y, "ぇ");
		ifontJpn:print(x*11, y, "ぉ");
		
		x=25;
		y=y + 20;
		
		ifontJpn:print(x*1, y, "か");
		ifontJpn:print(x*2, y, "き");
		ifontJpn:print(x*3, y, "く");
		ifontJpn:print(x*4, y, "け");
		ifontJpn:print(x*5, y, "こ");
		ifontJpn:print(x*7, y, "が");
		ifontJpn:print(x*8, y, "ぎ");
		ifontJpn:print(x*9, y, "ぐ");
		ifontJpn:print(x*10, y, "げ");
		ifontJpn:print(x*11, y, "ご");
		
		x=25;
		y=y + 20;
		
		ifontJpn:print(x*1, y, "さ");
		ifontJpn:print(x*2, y, "し");
		ifontJpn:print(x*3, y, "す");
		ifontJpn:print(x*4, y, "せ");
		ifontJpn:print(x*5, y, "そ");
		ifontJpn:print(x*7, y, "ざ");
		ifontJpn:print(x*8, y, "じ");
		ifontJpn:print(x*9, y, "ず");
		ifontJpn:print(x*10, y, "ぜ");
		ifontJpn:print(x*11, y, "ぞ");
		
		x=25;
		y=y + 20;
		
		ifontJpn:print(x*1, y, "た");
		ifontJpn:print(x*2, y, "ち");
		ifontJpn:print(x*3, y, "つ");
		ifontJpn:print(x*4, y, "て");
		ifontJpn:print(x*5, y, "と");
		ifontJpn:print(x*7, y, "だ");
		ifontJpn:print(x*8, y, "ぢ");
		ifontJpn:print(x*9, y, "づ");
		ifontJpn:print(x*10, y, "で");
		ifontJpn:print(x*11, y, "ど");
		ifontJpn:print(x*13, y, "っ");
	  
		x=25;
		y=y + 20;
		
		ifontJpn:print(x*1, y, "な");
		ifontJpn:print(x*2, y, "に");
		ifontJpn:print(x*3, y, "ぬ");
		ifontJpn:print(x*4, y, "ね");
		ifontJpn:print(x*5, y, "の");
		
		x=25;
		y=y + 20;
		
		ifontJpn:print(x*1, y, "は");
		ifontJpn:print(x*2, y, "ひ");
		ifontJpn:print(x*3, y, "ふ");
		ifontJpn:print(x*4, y, "へ");
		ifontJpn:print(x*5, y, "ほ");
		ifontJpn:print(x*7, y, "ば");
		ifontJpn:print(x*8, y, "び");
		ifontJpn:print(x*9, y, "ぶ");
		ifontJpn:print(x*10, y, "べ");
		ifontJpn:print(x*11, y, "ぼ");
		ifontJpn:print(x*13, y, "ぱ");
		ifontJpn:print(x*14, y, "ぴ");
		ifontJpn:print(x*15, y, "ぷ");
		ifontJpn:print(x*16, y, "ぺ");
		ifontJpn:print(x*17, y, "ぽ");
		
		x=25;
		y=y + 20;
		
		ifontJpn:print(x*1, y, "ま");
		ifontJpn:print(x*2, y, "み");
		ifontJpn:print(x*3, y, "む");
		ifontJpn:print(x*4, y, "め");
		ifontJpn:print(x*5, y, "も");
	  
		x=25;
		y=y + 20;
		
		ifontJpn:print(x*1, y, "や");
		ifontJpn:print(x*3, y, "ゆ");
		ifontJpn:print(x*5, y, "よ");
		ifontJpn:print(x*7, y, "ゃ");
		ifontJpn:print(x*9, y, "ゅ");
		ifontJpn:print(x*11, y, "ょ");
	  
		x=25;
		y=y + 20;
	  
		ifontJpn:print(x*1, y, "ら");
		ifontJpn:print(x*2, y, "り");
		ifontJpn:print(x*3, y, "る");
		ifontJpn:print(x*4, y, "れ");
		ifontJpn:print(x*5, y, "ろ");
	  
		x=25;
		y=y + 20;
		
		ifontJpn:print(x*1, y, "わ");
		ifontJpn:print(x*3, y, "を");
		ifontJpn:print(x*5, y, "ん");
		ifontJpn:print(x*7, y, "、");
		ifontJpn:print(x*8, y, "？");
		ifontJpn:print(x*9, y, "。");
		ifontJpn:print(x*10, y, "ー");
		
		else
		
		x=25;
		y=50;
		
		ifontJpn:print(x*1, y, "ア");
		ifontJpn:print(x*2, y, "イ");
		ifontJpn:print(x*3, y, "ウ");
		ifontJpn:print(x*4, y, "エ");
		ifontJpn:print(x*5, y, "オ");
		ifontJpn:print(x*7, y, "ァ");
		ifontJpn:print(x*8, y, "ィ");
		ifontJpn:print(x*9, y, "ゥ");
		ifontJpn:print(x*10, y, "ェ");
		ifontJpn:print(x*11, y, "ォ");
		ifontJpn:print(x*13, y, "ヴ");
		
		x=25;
		y=y+20;
		
		ifontJpn:print(x*1, y, "カ");
		ifontJpn:print(x*2, y, "キ");
		ifontJpn:print(x*3, y, "ク");
		ifontJpn:print(x*4, y, "ケ");
		ifontJpn:print(x*5, y, "コ");
		ifontJpn:print(x*7, y, "ガ");
		ifontJpn:print(x*8, y, "ギ");
		ifontJpn:print(x*9, y, "グ");
		ifontJpn:print(x*10, y, "ゲ");
		ifontJpn:print(x*11, y, "ゴ");
		
		x=25;
		y=y + 20;
		
		ifontJpn:print(x*1, y, "サ");
		ifontJpn:print(x*2, y, "シ");
		ifontJpn:print(x*3, y, "ス");
		ifontJpn:print(x*4, y, "セ");
		ifontJpn:print(x*5, y, "ソ");
		ifontJpn:print(x*7, y, "ザ");
		ifontJpn:print(x*8, y, "ジ");
		ifontJpn:print(x*9, y, "ズ");
		ifontJpn:print(x*10, y, "ゼ");
		ifontJpn:print(x*11, y, "ゾ");
		
		x=25;
		y=y + 20;
		
		ifontJpn:print(x*1, y, "タ");
		ifontJpn:print(x*2, y, "チ");
		ifontJpn:print(x*3, y, "ツ");
		ifontJpn:print(x*4, y, "テ");
		ifontJpn:print(x*5, y, "ト");
		ifontJpn:print(x*7, y, "ダ");
		ifontJpn:print(x*8, y, "ヂ");
		ifontJpn:print(x*9, y, "ヅ");
		ifontJpn:print(x*10, y, "デ");
		ifontJpn:print(x*11, y, "ド");
		ifontJpn:print(x*13, y, "ッ");
	  
		x=25;
		y=y + 20;
		
		ifontJpn:print(x*1, y, "ナ");
		ifontJpn:print(x*2, y, "ニ");
		ifontJpn:print(x*3, y, "ヌ");
		ifontJpn:print(x*4, y, "ネ");
		ifontJpn:print(x*5, y, "ノ");
	  
		x=25;
		y=y + 20;
		
		ifontJpn:print(x*1, y, "ハ");
		ifontJpn:print(x*2, y, "ヒ");
		ifontJpn:print(x*3, y, "フ");
		ifontJpn:print(x*4, y, "ヘ");
		ifontJpn:print(x*5, y, "ホ");
		ifontJpn:print(x*7, y, "バ");
		ifontJpn:print(x*8, y, "ビ");
		ifontJpn:print(x*9, y, "ブ");
		ifontJpn:print(x*10, y, "ベ");
		ifontJpn:print(x*11, y, "ボ");
		ifontJpn:print(x*13, y, "パ");
		ifontJpn:print(x*14, y, "ピ");
		ifontJpn:print(x*15, y, "プ");
		ifontJpn:print(x*16, y, "ペ");
		ifontJpn:print(x*17, y, "ポ");
		
		x=25;
		y=y + 20;
		
		ifontJpn:print(x*1, y, "マ");
		ifontJpn:print(x*2, y, "ミ");
		ifontJpn:print(x*3, y, "ム");
		ifontJpn:print(x*4, y, "メ");
		ifontJpn:print(x*5, y, "モ");
	  
		x=25;
		y=y + 20;
		
		ifontJpn:print(x*1, y, "ヤ");
		ifontJpn:print(x*3, y, "ユ");
		ifontJpn:print(x*5, y, "ヨ");
		ifontJpn:print(x*7, y, "ャ");
		ifontJpn:print(x*9, y, "ュ");
		ifontJpn:print(x*11, y, "ョ");
	  
		x=25;
		y=y + 20;
	  
		ifontJpn:print(x*1, y, "ラ");
		ifontJpn:print(x*2, y, "リ");
		ifontJpn:print(x*3, y, "ル");
		ifontJpn:print(x*4, y, "レ");
		ifontJpn:print(x*5, y, "ロ");
	  
		x=25;
		y=y + 20;
		
		ifontJpn:print(x*1, y, "ワ");
		ifontJpn:print(x*3, y, "ヲ");
		ifontJpn:print(x*5, y, "ン");
		ifontJpn:print(x*7, y, "、");
		ifontJpn:print(x*8, y, "？");
		ifontJpn:print(x*9, y, "。");
		ifontJpn:print(x*10, y, "ー");
		
		end
		
		ifontLtn:setStyle(0.7, lightblue, darkgray, IntraFont.ALIGN_CENTER);
		ifontLtn:print(240,250, "Cross: Write / Square: Search / Triangle: Delete / Circle: Clear")
		ifontLtn:setStyle(1.0, white, darkgray, IntraFont.ALIGN_CENTER);
		ifontLtn:print(30, 20, "[");
		ifontLtn:print(450, 20, "]");
		--ifontLtn:print(30, 20, "< L");
		--ifontLtn:print(450, 20, "R >");
		
		screen:blit(currentMouseX, currentMouseY, mouse);
		
		local pad = Controls.readPeek()
		
		if (pad:cross()) then
			screen.waitVblankStart(20);
			if (hiraganaMode and #searchArray < 22) then
				if (valueBetween(16, 35, 34, 52, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "あ";	
					table.insert(searchArray, "あ")
				elseif (valueBetween(40, 60, 35, 52, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "い";	
					table.insert(searchArray, "い")
				elseif (valueBetween(67, 84, 34, 52, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "う";	
					table.insert(searchArray, "う")
				elseif (valueBetween(92, 108, 34, 52, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "え";	
					table.insert(searchArray, "え")
				elseif (valueBetween(116, 135, 34, 52, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "お";	
					table.insert(searchArray, "お")
				elseif (valueBetween(167, 183, 36, 52, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ぁ";	
					table.insert(searchArray, "ぁ")
				elseif (valueBetween(191, 209, 36, 52, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ぃ";	
					table.insert(searchArray, "ぃ")
				elseif (valueBetween(218, 232, 36, 52, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ぅ";	
					table.insert(searchArray, "ぅ")
				elseif (valueBetween(242, 258, 36, 52, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ぇ";	
					table.insert(searchArray, "ぇ")
				elseif (valueBetween(267, 284, 36, 52, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ぉ";	
					table.insert(searchArray, "ぉ")
				elseif (valueBetween(15, 34, 54, 72, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "か";	
					table.insert(searchArray, "か")
				elseif (valueBetween(42, 59, 54, 72, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "き";	
					table.insert(searchArray, "き")
				elseif (valueBetween(66, 83, 54, 72, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "く";	
					table.insert(searchArray, "く")
				elseif (valueBetween(91, 110, 54, 72, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "け";	
					table.insert(searchArray, "け")
				elseif (valueBetween(117, 134, 54, 72, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "こ";	
					table.insert(searchArray, "こ")
				elseif (valueBetween(166, 185, 54, 72, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "が";	
					table.insert(searchArray, "が")
				elseif (valueBetween(192, 210, 54, 72, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ぎ";	
					table.insert(searchArray, "ぎ")
				elseif (valueBetween(217, 235, 54, 72, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ぐ";	
					table.insert(searchArray, "ぐ")
				elseif (valueBetween(242, 259, 54, 72, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "げ";	
					table.insert(searchArray, "げ")
				elseif (valueBetween(268, 285, 54, 72, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ご";	
					table.insert(searchArray, "ご")
				elseif (valueBetween(16, 35, 74, 92, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "さ";	
					table.insert(searchArray, "さ")
				elseif (valueBetween(41, 60, 74, 92, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "し";	
					table.insert(searchArray, "し")
				elseif (valueBetween(66, 84, 74, 92, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "す";	
					table.insert(searchArray, "す")
				elseif (valueBetween(90, 110, 74, 92, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "せ";	
					table.insert(searchArray, "せ")
				elseif (valueBetween(116, 135, 74, 92, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "そ";	
					table.insert(searchArray, "そ")
				elseif (valueBetween(166, 186, 74, 92, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ざ";	
					table.insert(searchArray, "ざ")
				elseif (valueBetween(192, 210, 74, 92, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "じ";	
					table.insert(searchArray, "じ")
				elseif (valueBetween(215, 235, 74, 92, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ず";	
					table.insert(searchArray, "ず")
				elseif (valueBetween(240, 260, 74, 92, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ぜ";	
					table.insert(searchArray, "ぜ")
				elseif (valueBetween(265, 285, 74, 92, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ぞ";	
					table.insert(searchArray, "ぞ")
				elseif (valueBetween(16, 35, 94, 112, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "た";	
					table.insert(searchArray, "た")
				elseif (valueBetween(41, 60, 94, 112, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ち";	
					table.insert(searchArray, "ち")
				elseif (valueBetween(66, 84, 94, 112, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "つ";	
					table.insert(searchArray, "つ")
				elseif (valueBetween(90, 110, 94, 112, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "て";	
					table.insert(searchArray, "て")
				elseif (valueBetween(116, 135, 94, 112, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "と";	
					table.insert(searchArray, "と")
				elseif (valueBetween(166, 186, 94, 112, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "だ";	
					table.insert(searchArray, "だ")
				elseif (valueBetween(192, 210, 94, 112, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ぢ";	
					table.insert(searchArray, "ぢ")
				elseif (valueBetween(215, 235, 94, 112, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "づ";	
					table.insert(searchArray, "づ")
				elseif (valueBetween(240, 260, 94, 112, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "で";	
					table.insert(searchArray, "で")
				elseif (valueBetween(265, 285, 94, 112, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ど";	
					table.insert(searchArray, "ど")
				elseif (valueBetween(316, 334, 98, 112, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "っ";	
					table.insert(searchArray, "っ")
				elseif (valueBetween(16, 35, 114, 133, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "な";	
					table.insert(searchArray, "な")
				elseif (valueBetween(41, 60, 114, 133, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "に";	
					table.insert(searchArray, "に")
				elseif (valueBetween(66, 84, 114, 133, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ぬ";	
					table.insert(searchArray, "ぬ")
				elseif (valueBetween(90, 110, 114, 133, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ね";	
					table.insert(searchArray, "ね")
				elseif (valueBetween(116, 135, 114, 133, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "の";	
					table.insert(searchArray, "の")
				elseif (valueBetween(16, 35, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "は";	
					table.insert(searchArray, "は")
				elseif (valueBetween(41, 60, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ひ";	
					table.insert(searchArray, "ひ")
				elseif (valueBetween(66, 84, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ふ";	
					table.insert(searchArray, "ふ")
				elseif (valueBetween(90, 110, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "へ";	
					table.insert(searchArray, "へ")
				elseif (valueBetween(116, 135, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ほ";	
					table.insert(searchArray, "ほ")
				elseif (valueBetween(166, 186, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ば";	
					table.insert(searchArray, "ば")
				elseif (valueBetween(192, 210, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "び";	
					table.insert(searchArray, "び")
				elseif (valueBetween(215, 235, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ぶ";	
					table.insert(searchArray, "ぶ")
				elseif (valueBetween(240, 260, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "べ";	
					table.insert(searchArray, "べ")
				elseif (valueBetween(265, 285, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ぼ";	
					table.insert(searchArray, "ぼ")
				elseif (valueBetween(317, 335, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ぱ";	
					table.insert(searchArray, "ぱ")
				elseif (valueBetween(341, 360, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ぴ";	
					table.insert(searchArray, "ぴ")
				elseif (valueBetween(365, 385, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ぷ";	
					table.insert(searchArray, "ぷ")
				elseif (valueBetween(390, 411, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ぺ";	
					table.insert(searchArray, "ぺ")
				elseif (valueBetween(416, 435, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ぽ";	
					table.insert(searchArray, "ぽ")
				elseif (valueBetween(16, 35, 154, 173, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ま";	
					table.insert(searchArray, "ま")
				elseif (valueBetween(41, 60, 154, 173, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "み";	
					table.insert(searchArray, "み")
				elseif (valueBetween(66, 84, 154, 173, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "む";	
					table.insert(searchArray, "む")
				elseif (valueBetween(90, 110, 154, 173, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "め";	
					table.insert(searchArray, "め")
				elseif (valueBetween(116, 135, 154, 173, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "も";	
					table.insert(searchArray, "も")
				elseif (valueBetween(16, 35, 174, 193, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "や";	
					table.insert(searchArray, "や")
				elseif (valueBetween(66, 84, 174, 193, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ゆ";	
					table.insert(searchArray, "ゆ")
				elseif (valueBetween(116, 135, 174, 193, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "よ";	
					table.insert(searchArray, "よ")
				elseif (valueBetween(166, 184, 177, 193, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ゃ";	
					table.insert(searchArray, "ゃ")
				elseif (valueBetween(217, 233, 177, 193, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ゅ";	
					table.insert(searchArray, "ゅ")
				elseif (valueBetween(268, 284, 177, 193, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ょ";	
					table.insert(searchArray, "ょ")
				elseif (valueBetween(16, 35, 194, 213, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ら";	
					table.insert(searchArray, "ら")
				elseif (valueBetween(41, 60, 194, 213, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "り";	
					table.insert(searchArray, "り")
				elseif (valueBetween(66, 84, 194, 213, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "る";	
					table.insert(searchArray, "る")
				elseif (valueBetween(90, 110, 194, 213, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "れ";	
					table.insert(searchArray, "れ")
				elseif (valueBetween(116, 135, 194, 213, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ろ";	
					table.insert(searchArray, "ろ")
				elseif (valueBetween(16, 35, 214, 233, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "わ";	
					table.insert(searchArray, "わ")
				elseif (valueBetween(66, 84, 214, 233, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "を";	
					table.insert(searchArray, "を")
				elseif (valueBetween(116, 135, 214, 233, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ん";	
					table.insert(searchArray, "ん")
				elseif (valueBetween(163, 177, 220, 233, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "、";	
					table.insert(searchArray, "、")
				elseif (valueBetween(191, 208, 213, 233, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "？";	
					table.insert(searchArray, "？")
				elseif (valueBetween(214, 226, 220, 233, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "。";	
					table.insert(searchArray, "。")
				elseif (valueBetween(239, 261, 217, 233, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ー";	
					table.insert(searchArray, "ー")
				end
				
			elseif (hiraganaMode == false and #searchArray < 22) then
				-- katakana
				if (valueBetween(16, 35, 34, 52, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ア";	
					table.insert(searchArray, "ア")
				elseif (valueBetween(40, 60, 35, 52, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "イ";	
					table.insert(searchArray, "イ")
				elseif (valueBetween(67, 84, 34, 52, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ウ";	
					table.insert(searchArray, "ウ")
				elseif (valueBetween(92, 108, 34, 52, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "エ";	
					table.insert(searchArray, "エ")
				elseif (valueBetween(116, 135, 34, 52, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "オ";	
					table.insert(searchArray, "オ")
				elseif (valueBetween(167, 183, 36, 52, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ァ";	
					table.insert(searchArray, "ァ")
				elseif (valueBetween(191, 209, 36, 52, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ィ";	
					table.insert(searchArray, "ィ")
				elseif (valueBetween(218, 232, 36, 52, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ゥ";	
					table.insert(searchArray, "ゥ")
				elseif (valueBetween(242, 258, 36, 52, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ェ";	
					table.insert(searchArray, "ェ")
				elseif (valueBetween(267, 284, 36, 52, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ォ";	
					table.insert(searchArray, "ォ")
				elseif (valueBetween(317, 335, 34, 52, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ヴ";	
					table.insert(searchArray, "ヴ")
				elseif (valueBetween(15, 34, 54, 72, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "カ";	
					table.insert(searchArray, "カ")
				elseif (valueBetween(42, 59, 54, 72, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "キ";	
					table.insert(searchArray, "キ")
				elseif (valueBetween(66, 83, 54, 72, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ク";	
					table.insert(searchArray, "ク")
				elseif (valueBetween(91, 110, 54, 72, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ケ";	
					table.insert(searchArray, "ケ")
				elseif (valueBetween(117, 134, 54, 72, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "コ";	
					table.insert(searchArray, "コ")
				elseif (valueBetween(166, 185, 54, 72, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ガ";	
					table.insert(searchArray, "ガ")
				elseif (valueBetween(192, 210, 54, 72, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ギ";	
					table.insert(searchArray, "ギ")
				elseif (valueBetween(217, 235, 54, 72, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "グ";	
					table.insert(searchArray, "グ")
				elseif (valueBetween(242, 259, 54, 72, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ゲ";	
					table.insert(searchArray, "ゲ")
				elseif (valueBetween(268, 285, 54, 72, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ゴ";	
					table.insert(searchArray, "ゴ")
				elseif (valueBetween(16, 35, 74, 92, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "サ";	
					table.insert(searchArray, "サ")
				elseif (valueBetween(41, 60, 74, 92, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "シ";	
					table.insert(searchArray, "シ")
				elseif (valueBetween(66, 84, 74, 92, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ス";	
					table.insert(searchArray, "ス")
				elseif (valueBetween(90, 110, 74, 92, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "セ";	
					table.insert(searchArray, "セ")
				elseif (valueBetween(116, 135, 74, 92, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ソ";	
					table.insert(searchArray, "ソ")
				elseif (valueBetween(166, 186, 74, 92, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ザ";	
					table.insert(searchArray, "ザ")
				elseif (valueBetween(192, 210, 74, 92, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ジ";	
					table.insert(searchArray, "ジ")
				elseif (valueBetween(215, 235, 74, 92, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ズ";	
					table.insert(searchArray, "ズ")
				elseif (valueBetween(240, 260, 74, 92, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ゼ";	
					table.insert(searchArray, "ゼ")
				elseif (valueBetween(265, 285, 74, 92, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ゾ";	
					table.insert(searchArray, "ゾ")
				elseif (valueBetween(16, 35, 94, 112, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "タ";	
					table.insert(searchArray, "タ")
				elseif (valueBetween(41, 60, 94, 112, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "チ";	
					table.insert(searchArray, "チ")
				elseif (valueBetween(66, 84, 94, 112, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ツ";	
					table.insert(searchArray, "ツ")
				elseif (valueBetween(90, 110, 94, 112, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "テ";	
					table.insert(searchArray, "テ")
				elseif (valueBetween(116, 135, 94, 112, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ト";	
					table.insert(searchArray, "ト")
				elseif (valueBetween(166, 186, 94, 112, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ダ";	
					table.insert(searchArray, "ダ")
				elseif (valueBetween(192, 210, 94, 112, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ヂ";	
					table.insert(searchArray, "ヂ")
				elseif (valueBetween(215, 235, 94, 112, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ヅ";	
					table.insert(searchArray, "ヅ")
				elseif (valueBetween(240, 260, 94, 112, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "デ";	
					table.insert(searchArray, "デ")
				elseif (valueBetween(265, 285, 94, 112, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ド";	
					table.insert(searchArray, "ド")
				elseif (valueBetween(316, 334, 98, 112, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ッ";	
					table.insert(searchArray, "ッ")
				elseif (valueBetween(16, 35, 114, 133, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ナ";	
					table.insert(searchArray, "ナ")
				elseif (valueBetween(41, 60, 114, 133, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ニ";	
					table.insert(searchArray, "ニ")
				elseif (valueBetween(66, 84, 114, 133, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ヌ";	
					table.insert(searchArray, "ヌ")
				elseif (valueBetween(90, 110, 114, 133, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ネ";	
					table.insert(searchArray, "ネ")
				elseif (valueBetween(116, 135, 114, 133, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ノ";	
					table.insert(searchArray, "ノ")
				elseif (valueBetween(16, 35, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ハ";	
					table.insert(searchArray, "ハ")
				elseif (valueBetween(41, 60, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ヒ";	
					table.insert(searchArray, "ヒ")
				elseif (valueBetween(66, 84, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "フ";	
					table.insert(searchArray, "フ")
				elseif (valueBetween(90, 110, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ヘ";	
					table.insert(searchArray, "ヘ")
				elseif (valueBetween(116, 135, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ホ";	
					table.insert(searchArray, "ホ")
				elseif (valueBetween(166, 186, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "バ";	
					table.insert(searchArray, "バ")
				elseif (valueBetween(192, 210, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ビ";	
					table.insert(searchArray, "ビ")
				elseif (valueBetween(215, 235, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ブ";	
					table.insert(searchArray, "ブ")
				elseif (valueBetween(240, 260, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ベ";	
					table.insert(searchArray, "ベ")
				elseif (valueBetween(265, 285, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ボ";	
					table.insert(searchArray, "ボ")
				elseif (valueBetween(317, 335, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "パ";	
					table.insert(searchArray, "パ")
				elseif (valueBetween(341, 360, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ピ";	
					table.insert(searchArray, "ピ")
				elseif (valueBetween(365, 385, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "プ";	
					table.insert(searchArray, "プ")
				elseif (valueBetween(390, 411, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ペ";	
					table.insert(searchArray, "ペ")
				elseif (valueBetween(416, 435, 134, 153, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ポ";	
					table.insert(searchArray, "ポ")
				elseif (valueBetween(16, 35, 154, 173, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "マ";	
					table.insert(searchArray, "マ")
				elseif (valueBetween(41, 60, 154, 173, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ミ";	
					table.insert(searchArray, "ミ")
				elseif (valueBetween(66, 84, 154, 173, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ム";	
					table.insert(searchArray, "ム")
				elseif (valueBetween(90, 110, 154, 173, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "メ";	
					table.insert(searchArray, "メ")
				elseif (valueBetween(116, 135, 154, 173, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "モ";	
					table.insert(searchArray, "モ")
				elseif (valueBetween(16, 35, 174, 193, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ヤ";	
					table.insert(searchArray, "ヤ")
				elseif (valueBetween(66, 84, 174, 193, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ユ";	
					table.insert(searchArray, "ユ")
				elseif (valueBetween(116, 135, 174, 193, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ヨ";	
					table.insert(searchArray, "ヨ")
				elseif (valueBetween(166, 184, 177, 193, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ャ";	
					table.insert(searchArray, "ャ")
				elseif (valueBetween(217, 233, 177, 193, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ュ";	
					table.insert(searchArray, "ュ")
				elseif (valueBetween(268, 284, 177, 193, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ョ";	
					table.insert(searchArray, "ョ")
				elseif (valueBetween(16, 35, 194, 213, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ラ";	
					table.insert(searchArray, "ラ")
				elseif (valueBetween(41, 60, 194, 213, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "リ";	
					table.insert(searchArray, "リ")
				elseif (valueBetween(66, 84, 194, 213, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ル";	
					table.insert(searchArray, "ル")
				elseif (valueBetween(90, 110, 194, 213, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "レ";	
					table.insert(searchArray, "レ")
				elseif (valueBetween(116, 135, 194, 213, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ロ";	
					table.insert(searchArray, "ロ")
				elseif (valueBetween(16, 35, 214, 233, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ワ";	
					table.insert(searchArray, "ワ")
				elseif (valueBetween(66, 84, 214, 233, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ヲ";	
					table.insert(searchArray, "ヲ")
				elseif (valueBetween(116, 135, 214, 233, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ン";	
					table.insert(searchArray, "ン")
				elseif (valueBetween(163, 177, 220, 233, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "、";	
					table.insert(searchArray, "、")
				elseif (valueBetween(191, 208, 213, 233, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "？";	
					table.insert(searchArray, "？")
				elseif (valueBetween(214, 226, 220, 233, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "。";	
					table.insert(searchArray, "。")
				elseif (valueBetween(239, 261, 217, 233, currentMouseX, currentMouseY)) then
					searchWord = searchWord .. "ー";	
					table.insert(searchArray, "ー")
				end
			end
		elseif (pad:analogY() < -50 and pad:analogX() <-50) then
			currentMouseY = currentMouseY - 1;
			currentMouseX = currentMouseX - 1;
			if (currentMouseY < 0) then
				currentMouseY = 0;
			end
			if (currentMouseX < 0) then
				currentMouseX = 0;
			end
		elseif (pad:analogY() < -50 and pad:analogX() > 50) then
			currentMouseY = currentMouseY - 1;
			currentMouseX = currentMouseX + 1;
			if (currentMouseY < 0) then
				currentMouseY = 0;
			end
			if (currentMouseX > 470) then
				currentMouseX = 470;
			end
		elseif (pad:analogY() > 50 and pad:analogX() <-50) then
			currentMouseY = currentMouseY + 1;
			currentMouseX = currentMouseX - 1;
			if (currentMouseY > 262) then
				currentMouseY = 262;
			end
			if (currentMouseX < 0) then
				currentMouseX = 0;
			end
		elseif (pad:analogY() > 50 and pad:analogX() > 50) then
			currentMouseY = currentMouseY + 1;
			currentMouseX = currentMouseX + 1;
			if (currentMouseY > 262) then
				currentMouseY = 262;
			end
			if (currentMouseX > 470) then
				currentMouseX = 470;
			end
		elseif (pad:analogY() < -50 ) then
			currentMouseY = currentMouseY - 1;
			if (currentMouseY < 0) then
				currentMouseY = 0;
			end
		elseif (pad:analogY() > 50 ) then
			currentMouseY = currentMouseY + 1;
			if (currentMouseY > 262) then
				currentMouseY = 262;
			end
		elseif (pad:analogX() < -50 ) then
			currentMouseX = currentMouseX - 1;
			if (currentMouseX < 0) then
				currentMouseX = 0;
			end
		elseif (pad:analogX() > 50 ) then
			currentMouseX = currentMouseX + 1;
			if (currentMouseX > 470) then
				currentMouseX = 470;
			end
		elseif (pad:up()) then
			currentMouseY = currentMouseY - 2;
			if (currentMouseY > 262) then
				currentMouseY = 262;
			end
		elseif (pad:down()) then
			currentMouseY = currentMouseY + 2;
			if (currentMouseY < 0) then
				currentMouseY = 0;
			end
		elseif (pad:left()) then
			currentMouseX = currentMouseX - 2;
			if (currentMouseX < 0) then
				currentMouseX = 0;
			end
		elseif (pad:right()) then
			currentMouseX = currentMouseX + 2;
			if (currentMouseX > 470) then
				currentMouseX = 470;
			end
		elseif (pad:square()) then
			screen.waitVblankStart(20);
			break
		elseif (pad:circle()) then
			searchWord = "";
			searchArray = {}
			screen.waitVblankStart(20);
		elseif (pad:triangle()) then
			if (#searchArray > 0) then
				table.remove(searchArray);
				searchWord = table.concat(searchArray)
			end
			screen.waitVblankStart(20);
		
		-- There may not be enough memory for changing the type?
		--[[elseif (pad:r()) then
			if (hiraganaMode) then
				hiraganaMode = false;
			else
				hiraganaMode = true;
			end
			screen.waitVblankStart(20);
		elseif (pad:l()) then
			if (hiraganaMode) then
				hiraganaMode = false;
			else
				hiraganaMode = true;
			end
			screen.waitVblankStart(20);]]
		elseif (pad:home()) then
			break;
		elseif pad:select() then
			screen.waitVblankStart(20);
			if (questionImage ~= nil and questionImage ~= false) then -- free memory
				Image.free(questionImage)
			end
			askLevel = 0;
			selectDictionary();
			
		elseif pad:start() then
			screen.waitVblankStart(20);
			openOptions();
		end -- end for pad
	
	  System.endDraw()
	  screen.flip()
	  
	  end -- end for while
	  
	  return searchWord
end

return keyboard