Jump to content

Trying to detect enter key press for ISTextEntryBox


ethanwdp

Recommended Posts

I've been stuck on this one part. So far, the rest of the development has gone smoothly, but I've noticed that the keypress event doesn't fire when you're typing into an ISTextEntryBox, meaning that I can't hook an event to cause it to clear, unfocus, and do a thing when enter is pushed.

 

Code:

 

LMSWindow = ISPanel:derive("LMSWindow");

function LMSWindow:initialise()
    ISPanel.initialise(self);
end

function LMSWindow:new(x, y, width, height)
    local o = {};
    o = ISPanel:new(x, y, width, height);
    setmetatable(o, self);
    self.__index = self;
    o.title = "";
    o.pin = false;
    o:noBackground();
    return o;
end

function LMSWindow:createChildren()
    ISPanel.createChildren(self);
    self.LMSchatBar = ISTextEntryBox:new("", 0, 15, 300, 20)
    self.LMSchatBar:initialise();
    self:addChild(self.LMSchatBar)
end

function LMSspawn()
    LMSWindow = LMSWindow:new(35, getCore():getScreenHeight() - 100, 300, 40)
    LMSWindow:addToUIManager();
    LMSWindow:setVisible(false);
end

function KeyListener(_keyPressed)
    if _keyPressed == 20 then
          if not LMSWindow:getIsVisible() then
              LMSWindow:setVisible(true);
          else
              LMSWindow:setVisible(false);
          end
    end
    if _keyPressed == 28 and LMSWindow.LMSchatBar:getText() then
        local LMStext = LMSWindow.LMSchatBar:getText()
        if not string.find(LMStext, "/", 1) then
            LMSWindow:unfocus()
            getPlayer():Say(LMStext)
            LMSWindow.LMSchatBar:clear()
        end
    end
end

function getLMSText()
    if LMSWindow:getIsVisible() then
        if LMSWindow.LMSchatBar:getText() then
            -- this ended up not working, as I still couldn't detect a key press
        end
    end
end

Events.OnKeyPressed.Add(KeyListener);

Events.OnGameStart.Add(LMSspawn);

Events.OnTick.Add(getLMSText);

 

How do I get the keypress event to fire in this case?

Link to comment
Share on other sites

EDIT:

Figured it out.

I decided to modify the original ISTextEntryBox, and make the onCommandEntered() function do something.

My hunch was right, it DOES invoke onCommandEntered() properly, as it's just mirroring a few Java files.

When I made it do something, like print, it actually worked. I'm guessing that's because the default function is empty, probably because making it return true makes horrors happen.

 

So I added this line to my code:

 

function ISTextEntryBox:onCommandEntered()
    print("test")
end

And it worked! It seemed to override the default ISTextEntryBox:onCommandEntered(), and I didn't have to include an override file.

 

After doing some tweaking, here's the new (working) code:

 

LMSWindow = ISPanel:derive("LMSWindow");

function LMSWindow:initialise()
    ISPanel.initialise(self);
end

function LMSWindow:new(x, y, width, height)
    local o = {};
    o = ISPanel:new(x, y, width, height);
    setmetatable(o, self);
    self.__index = self;
    o.title = "";
    o.pin = false;
    o:noBackground();
    return o;
end

function LMSWindow:createChildren()
    ISPanel.createChildren(self);
    self.LMSchatBar = ISTextEntryBox:new("", 0, 15, 300, 20)
    self.LMSchatBar:initialise();
    self:addChild(self.LMSchatBar)
end

function LMScreate()
    LMSWindow = LMSWindow:new(35, getCore():getScreenHeight() - 100, 300, 40)
    LMSWindow:addToUIManager();
    LMSWindow:setVisible(false);
end

function KeyListener(_keyPressed)
    if _keyPressed == 20 and not LMSWindow.LMSchatBar:getText() ~= nil then
          if not LMSWindow:getIsVisible() then
              LMSWindow:setVisible(true);
            LMSWindow.LMSchatBar:focus()
          else
              LMSWindow:setVisible(false);
            LMSWindow.LMSchatBar:clear()
          end
    end
end
function ISTextEntryBox:onCommandEntered()
    local LMStext = LMSWindow.LMSchatBar:getText()
    if not string.find(LMStext, "/", 1) then
        getPlayer():Say(LMStext);
        LMSWindow.LMSchatBar:clear();
        LMSWindow.LMSchatBar:unfocus();
        LMSWindow:setVisible(false);
    end
end
Events.OnGameStart.Add(LMScreate);

Events.OnKeyPressed.Add(KeyListener);

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...