Jump to content

How to know which key is pressed?


TEPA6ANT
 Share

Recommended Posts

i used to do something like this...

this will only add keyinfo to the Keys table when a key is actually used.
to make it work you would need to add a call to PreUpdate() in your main loop.
to check a key's state you would call eg: IsKeyUp(Key.W) or IsKeyDown(Key.W).

function SetKeyboardInfoPack()
	local pack = {
		Keys = {},
		--
		FlushKeyInfo = function (self, resetHit, key)
			if key ~= nil then
				local nHit = 0
				if self.Keys[key] ~= nil and resetHit == false then
					nHit = self.Keys[key].hit
				end
				self.Keys[key] = {down = false, up = false, hit = nHit}
			else 
				local nHit = 0
				for key,value in pairs(self.Keys) do 
					if resetHit == false then
						nHit = self.Keys[key].hit
					end
					self.Keys[key] = {down = false, up = false, hit = nHit}
				end
			end
		end,
		--
		PreUpdate = function (self)
			self:RefreshKeyStates()
		end,
		--
		IsKeyDown = function (self, key)
			--create a key record if not already exists and check the keystate
			if self.Keys[key] == nil then
				self.Keys[key] = {down = false, up = false, hit = 0}
				self:RefreshKey(key)
			end
			--return keystate
			return self.Keys[key].down
		end,
		--
		IsKeyUp = function (self, key)
			--create a key record if not already exists and check the keystate
			if self.Keys[key] == nil then
				self.Keys[key] = {down = false, up = false, hit = 0}
				self:RefreshKey(key)
			end
			--return keystate
			return self.Keys[key].up
		end,
		Keyhits = function (self, key)
			--create a key record if not already exists and check the keystate
			if self.Keys[key] == nil then
				self.Keys[key] = {down = false, up = false, hit = 0}
				self:RefreshKey(key)
			end
			--return keystate
			return self.Keys[key].hit
		end,
		--
		RefreshKeyStates = function (self)
			for key,value in pairs(self.Keys) do 
				self:RefreshKey(key)
			end
		end,
		RefreshKey = function (self, key)
			if key ~= nil then
				if self.Keys[key] ~= nil then
					if window:KeyDown(key) then
						self.Keys[key].down = true
						self.Keys[key].up = false
					elseif self.Keys[key].down == true then		--last state down
						self.Keys[key].down = false
						self.Keys[key].up = true
						self.Keys[key].hit = self.Keys[key].hit + 1
					elseif self.Keys[key].up == true then		--last state up
						self.Keys[key].down = false
						self.Keys[key].up = false
					end
				end
			end
		end
	}
	--
	return pack
end

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...