Murga-Projects Forums

Full Version: How to bind a key event for widget?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i want to bind a key event function for a input widget
to check if user has press the enter key
so how to do that??

Code:
do widget= fltk:Fl_Input(0,0,0,0,"")
widget:callback(input_cb)
widget:when(8) -- this is the constant for the Enter key


Don't use that code exactly as its missing dimensions and labels
But the widget:when(8) is what you are looking for.

There is a list of constants for events and I always forget where they are, either somewhere in the forums, in murgaLua examples, or maybe in Mikshaws widget example.

I use Fluid and then convert to lua so thats how I get it done, usually.

MrBill Wrote:
I use Fluid and then convert to lua so thats how I get it done, usually.


You just beat me to it :-)

The verbose version would be :

Code:
widget:when(fltk.FL_WHEN_ENTER_KEY_ALWAYS)

thank you very much
when i dont get the answer i use set_event_handler to solver the problem
the code is

Code:
function quit_callback(msg)
   if fltk.fl_choice("Are you sure you want to EXIT ?", "No", "Yes", nil) >= 1 then
      w:hide()
      os.exit()
   end
end

function all_event(event)
  if(Fl:event_key(FL_Enter) and murgaLua.getFltkEventText(event)=="FL_SHORTCUT") then
    msgout:add(msgin:value())
    msgout:bottomline(msgout:size())
    msgin:value("")
  end
end

ww,wh,wt=300,280,"Simple IRC Client - Powered by jyf1987"

w= fltk:Fl_Window(ww,wh,wt)
w:callback(quit_callback)

msgout= fltk:Fl_Browser(5, 5, 290, 240)

msgin= fltk:Fl_Input(5, 250, 290, 25)





w:show()

Fl.set_event_handler(all_event)
Fl:start_event_handler()
Fl:run()


also it's easy to use widget:when(fltk.FL_WHEN_ENTER_KEY_ALWAYS)

here is the new code

Code:
function quit_callback(msg)
   if fltk.fl_choice("Are you sure you want to EXIT ?", "No", "Yes", nil) >= 1 then
      w:hide()
      os.exit()
   end
end

ww,wh,wt=300,280,"Simple IRC Client - Powered by jyf1987"

w= fltk:Fl_Window(ww,wh,wt)
w:callback(quit_callback)

msgout= fltk:Fl_Browser(5, 5, 290, 240)


msgin= fltk:Fl_Input(5, 250, 290, 25)
msgin:callback(
function()
  msgout:add(msgin:value())
  msgout:bottomline(msgout:size())
  msgin:value("")
end
)
msgin:when(fltk.FL_WHEN_ENTER_KEY_ALWAYS)


w:show()

Fl:run()

jyf1987 Wrote:
thank you very much
when i dont get the answer i use set_event_handler to solver the problem

Great examples, thanks :-)

The only problem with using the event handler is that input fields will absorb the enter key (as a tab to next field) unless you mess around with when (say a when(0)).

Another tip is to use event_inside(widget), as this allows you (when using the event_handler) to know what widget you where in when the event occured.

Cheers
JohnM

thx for the tip
so anyother secret? Big Grin
i'am looking forward fltk2 to be support
for displaying chinese chars,any way,even that wont be support the murgalua is still a powerful tool for me,good works

jyf1987 Wrote:
thx for the tip
so anyother secret? Big Grin


Lots ... That's why I have to write some updated docs :-)

Cheers
JohnM

Although I'm not sure how well this would work for your needs, if at all, you could try using decimal codes to display the Chinese characters. It isn't ideal, of course, since it will not translate to other machines that don't have the exact font, and without some special magic it is probably useful only for static strings.

Here is a script that will display 256 characters available in the FL_SCREEN font. You'll need to add a set_font line to get your desired font:
Fl:set_font(fltk.FL_SCREEN,"my_font_name")
Optionally change bro:textfont() to a different constant.

EDIT: Then again, maybe just changing the font would provide the same level of usefulness/uselessness

Code:
odd=nil
ww=500; wh=350
win=fltk:Fl_Window(ww,wh,"decimal codes")
bro=fltk:Fl_Browser(10,10,ww/2-20,wh-20)
bro:textfont(fltk.FL_SCREEN)
for i=0,255 do
if i<10 then t="  " elseif i<100 then t=" " else t="" end
if odd then odd=nil; col="@B52@." else odd=1; col="" end
bro:add(col..t..i.."\t"..string.format('%c',i))
end
box=fltk:Fl_Multiline_Output(ww/2,10,ww/2-10,wh-20)
box:textsize(12)
box:set_output()
box:value([[
When including an escaped string of
1, 2, or 3 decimals as part of a Lua
string, those decimals will be read
as the character matching that code.
For example, \64 shows '@' and \10 is
a newline character on systems that
use ASCII:97

In this particular demo, the lines in
the browser were added programatically
rather than being provided as a full
string. Concatenating an escape character
to the string it is intended to escape
is seemingly impossible, but you can get
the same result with string.format:
string.format('%c',number)

Results may vary depending on your
system's configuration or chosen font.
]])
win:show()
Fl:run()

it is cool ,but chinese char use 2 bytes so 256 will not enough
Pages: 1 2
Reference URL's