AndyObtiva / glimmer-dsl-libui

Glimmer DSL for LibUI - Prerequisite-Free Ruby Desktop Development Cross-Platform Native GUI Library - The Quickest Way From Zero To GUI - If You Liked Shoes, You'll Love Glimmer! - No need to pre-install any prerequisites. Just install the gem and have platform-independent GUI that just works on Mac, Windows, and Linux.
MIT License
497 stars 15 forks source link

on_key_event? Formfield- Submit Form with Enter? #26

Closed Delikt closed 2 years ago

Delikt commented 2 years ago

Is it possible to input Userdata in a Textfield (Login Form) and submit it with pressing Enter instead of building a "Login" Button to submit Login?

a Example snippet will be welcome.

Thanks!

AndyObtiva commented 2 years ago

entry does not currently support observing keyboard events (like hitting ENTER). However, you can fake this behavior by using a non_wrapping_multiline_entry and testing for the presence of \n in the text property value (just make sure to remove \n from the text after confirming its presence to avoid showing a new line to the user).

Example:

require 'glimmer-dsl-libui'

include Glimmer

window('Entry Submission with Enter', 300, 50) {
  non_wrapping_multiline_entry { |nwme|
    on_changed do
      if nwme.text.include?("\n")
        nwme.text = nwme.text.gsub("\n", '')
        msg_box('Submission', "You entered:\n #{nwme.text}")
      end
    end
  }
}.show

Screen Shot 2022-05-16 at 9 20 40 AM

Screen Shot 2022-05-16 at 9 20 52 AM

Delikt commented 2 years ago

Thank you for your fast response, i will try around with that but i'm pretty sure this will not work cause i have a password_entry Field from where i want to submit.. i will reply here if the workaround worked for me

AndyObtiva commented 2 years ago

Yeah, no... that only works for standard text entry because non_wrapping_multiline_entry does not support a password mask like password_entry.

However, a workaround is to add a submit button below the password_entry, which the user can invoke using the keyboard alone with the following keys:

  1. TAB
  2. SPACE
require 'glimmer-dsl-libui'

include Glimmer

window('Submit with Keyboard', 250, 40) {
  vertical_box {
    password_entry {
    }
    button {
      text 'Submit'

      on_clicked do
        msg_box 'Submitted', 'Hitting tab and space triggers the submit button'
      end
    }
  }
}.show

Screen Shot 2022-05-16 at 10 46 15 AM

Screen Shot 2022-05-16 at 10 46 19 AM

Otherwise, there is one more workaround.

You can build your own ENTER-supporting custom password control using the area control (which supports arbitrary keyboard and mouse event listeners). If you really need it and need help building it, let me know, and I'd be happy to provide some code.

AndyObtiva commented 2 years ago

Hahaha, I just discovered one more workaround. You can attempt to mask the non_wrapping_multiline_entry yourself. I am including an example below, but it is not complete as it does not handle backspacing, deleting, moving the keyboard caret, and other edge cases, but it's just a proof of concept.

require 'glimmer-dsl-libui'

include Glimmer

window('Entry Submission with Enter', 300, 50) {
  vertical_box {
    non_wrapping_multiline_entry { |nwme|
      on_changed do
        if nwme.text.include?("\n")
          nwme.text = nwme.text.gsub("\n", '')
          msg_box('Submission', "You entered:\n #{@saved}")
        end
        if nwme.text.match(/[^*]/)
          @saved ||= ''
          @saved += nwme.text.match(/([^*])/)[1]
          nwme.text = nwme.text.gsub(/[^*]/, '*')
        end
      end
    }
  }
}.show

Screen Shot 2022-05-16 at 10 53 14 AM

Screen Shot 2022-05-16 at 10 53 17 AM

If you like this option, and need help completing it, let me know, and I'd be happy to help. Otherwise, let me know if you prefer the area based solution suggested in my last comment, or if the initial workaround of letting users submit by hitting TAB and SPACE (instead of ENTER) is good enough.

Delikt commented 2 years ago

Thank you very much for your Time and this workarounds, in my pov it make not more sense to press TAB and SPACE instead of TAB and Enter and the behavor not to handle backspacing, deleting, moving isn't that pretty... so i will let it is like it is for now...

Here a preview of my first Programm ever, what i builded with Ruby, LibUI, glimmer-dsl-libui and ocra to compile a self executable Windows exe:

Peek 2022-05-18 10-34

Thanks to all the Programers from the above mentationed Repos, who makes this happen!

This Program is a configurable Setup what i coded for my Work in my spare Time - it save me as IT Support a lot of Time.

Sadly, i can't puplish the Source, cause there are some stuff in the Code from my Company.