Murga-Projects Forums
How to bind a key event for widget? - Printable Version

+- Murga-Projects Forums (http://www.murga-projects.com/forum)
+-- Forum: Project Forums (/forumdisplay.php?fid=1)
+--- Forum: MurgaLua - General (/forumdisplay.php?fid=2)
+--- Thread: How to bind a key event for widget? (/showthread.php?tid=342)


How to bind a key event for widget? - jyf1987 - 09-13-2008 11:45 PM

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??


RE: How to bind a key event for widget? - MrBill - 09-14-2008 07:03 AM

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.


RE: How to bind a key event for widget? - JohnMurga - 09-14-2008 07:22 AM

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)




RE: How to bind a key event for widget? - jyf1987 - 09-14-2008 12:55 PM

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()




RE: How to bind a key event for widget? - JohnMurga - 09-15-2008 12:13 AM

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


RE: How to bind a key event for widget? - jyf1987 - 09-15-2008 10:37 PM

thx for the tip
so anyother secret? Big Grin


RE: How to bind a key event for widget? - jyf1987 - 09-15-2008 10:42 PM

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


RE: How to bind a key event for widget? - JohnMurga - 09-15-2008 11:37 PM

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


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

Cheers
JohnM


RE: How to bind a key event for widget? - mikshaw - 09-15-2008 11:39 PM

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()




RE: How to bind a key event for widget? - jyf1987 - 09-16-2008 12:21 AM

it is cool ,but chinese char use 2 bytes so 256 will not enough


RE: How to bind a key event for widget? - JohnMurga - 09-16-2008 04:38 AM

jyf1987 Wrote:
it is cool ,but chinese char use 2 bytes so 256 will not enough


I found a UTF8 patch for nightly FLTK 1.1.8 build ... I will play around with this, I should be able to make the current murgaLua UTF8 compatible quite easily.

The challenge will be merging the changes from there into the final 1.1.9 version ... Fun :-)

Either way I could definitely distribute multibyte capable binaries.

Cheers
JohnM


RE: How to bind a key event for widget? - jyf1987 - 09-16-2008 02:11 PM

great,i will wait for the good news